Add custom jest matcher for block id duplicate tests

pull/9133/head
Joshua Munn 2022-09-01 17:35:44 +01:00 zatwierdzone przez LB (Ben Johnston)
rodzic 94d6ca055f
commit 78fe000b19
6 zmienionych plików z 58 dodań i 38 usunięć
client
src
entrypoints/contrib/typed_table_block

Wyświetl plik

@ -278,9 +278,10 @@ describe('telepath: wagtail.blocks.ListBlock', () => {
test('duplicated blocks have unique ids', () => {
boundBlock.duplicateBlock(0);
expect(boundBlock.children[1].id).not.toBeUndefined();
expect(boundBlock.children[1].id).not.toBeNull();
expect(boundBlock.children[1].id).not.toEqual(boundBlock.children[0].id);
expect(boundBlock.children[1]).not.toHaveSameBlockIdAs(
boundBlock.children[0],
);
});
test('blocks can be split', () => {
@ -544,11 +545,8 @@ describe('telepath: wagtail.blocks.ListBlock with StreamBlock child', () => {
const originalStreamBlock = boundBlock.children[0].block;
// Test the ids on the duplicated stream child of the stream-block-in-list-block
expect(duplicatedStreamBlock.children[0].id).not.toBeUndefined();
expect(duplicatedStreamBlock.children[0].id).not.toBeNull();
expect(duplicatedStreamBlock.children[0].id).not.toEqual(
originalStreamBlock.children[0].id,
expect(duplicatedStreamBlock.children[0]).not.toHaveSameBlockIdAs(
originalStreamBlock.children[0],
);
});
});
@ -628,14 +626,10 @@ describe('telepath: wagtail.blocks.ListBlock inside a StreamBlock', () => {
const originalStreamChild = boundBlock.children[0];
const duplicatedStreamChild = boundBlock.children[1];
expect(duplicatedStreamChild.id).not.toBeNull();
expect(duplicatedStreamChild.id).not.toBeUndefined();
expect(duplicatedStreamChild.id).not.toEqual(originalStreamChild.id);
expect(duplicatedStreamChild).not.toHaveSameBlockIdAs(originalStreamChild);
expect(duplicatedStreamChild.block.children[0].id).not.toBeNull();
expect(duplicatedStreamChild.block.children[0].id).not.toBeUndefined();
expect(duplicatedStreamChild.block.children[0].id).not.toEqual(
originalStreamChild.block.children[0].id,
expect(duplicatedStreamChild.block.children[0]).not.toHaveSameBlockIdAs(
originalStreamChild.block.children[0],
);
});
});

Wyświetl plik

@ -423,12 +423,12 @@ describe('telepath: wagtail.blocks.StreamBlock with nested stream block', () =>
const duplicatedStreamChild = boundBlock.children[1];
const originalStreamChild = boundBlock.children[0];
expect(duplicatedStreamChild.id).not.toBeNull();
expect(duplicatedStreamChild.id).not.toBeUndefined();
expect(duplicatedStreamChild.id).not.toEqual(originalStreamChild.id);
// Test the outermost stream child
expect(duplicatedStreamChild).not.toHaveSameBlockIdAs(originalStreamChild);
expect(duplicatedStreamChild.block.children[0].id).not.toEqual(
originalStreamChild.block.children[0].id,
// Test the nested child
expect(duplicatedStreamChild.block.children[0]).not.toHaveSameBlockIdAs(
originalStreamChild.block.children[0],
);
});
});

Wyświetl plik

@ -421,26 +421,20 @@ describe('telepath: wagtail.blocks.StructBlock in stream block', () => {
});
test('ids are not duplicated when duplicating struct blocks', () => {
const testIds = (oldChild, newChild) => {
expect(newChild.id).not.toBeNull();
expect(newChild.id).not.toBeUndefined();
expect(newChild.id).not.toEqual(oldChild.id);
};
boundBlock.children[0].duplicate();
const duplicatedStreamChild = boundBlock.children[1];
const originalStreamChild = boundBlock.children[0];
testIds(originalStreamChild, duplicatedStreamChild);
expect(duplicatedStreamChild).not.toHaveSameBlockIdAs(originalStreamChild);
const duplicatedStreamBlockInStruct =
duplicatedStreamChild.block.childBlocks.inner_stream;
const originalStreamBlockInStruct =
originalStreamChild.block.childBlocks.inner_stream;
testIds(
expect(duplicatedStreamBlockInStruct.children[0]).not.toHaveSameBlockIdAs(
originalStreamBlockInStruct.children[0],
duplicatedStreamBlockInStruct.children[0],
);
});
});

Wyświetl plik

@ -324,18 +324,15 @@ describe('wagtail.contrib.typed_table_block.blocks.TypedTableBlock in StreamBloc
boundBlock.duplicateBlock(0);
// Check the ids on the top level blocks
expect(boundBlock.children[1].id).not.toBeNull();
expect(boundBlock.children[1].id).not.toEqual(boundBlock.children[0].id);
expect(boundBlock.children[1]).not.toHaveSameBlockIdAs(
boundBlock.children[0],
);
// Check the ids on the nested blocks
expect(
boundBlock.children[1].block.rows[0].blocks[0].children[0].id,
).not.toBeNull();
expect(
boundBlock.children[1].block.rows[0].blocks[0].children[0].id,
).not.toEqual(
boundBlock.children[0].block.rows[0].blocks[0].children[0].id,
boundBlock.children[1].block.rows[0].blocks[0].children[0],
).not.toHaveSameBlockIdAs(
boundBlock.children[0].block.rows[0].blocks[0].children[0],
);
});
});

Wyświetl plik

@ -0,0 +1,32 @@
expect.extend({
toHaveSameBlockIdAs(received, otherChild) {
const { id: thisId } = received;
const { id: otherId } = otherChild;
if (thisId === undefined || thisId === null) {
return {
message: 'expected block id not to be null or undefined',
pass: false,
};
}
if (otherId === undefined || otherId === null) {
return {
message: 'expected other block id not to be null or undefined',
pass: false,
};
}
return thisId === otherId
? {
message: () =>
`expected block id '${thisId}' not to match other id '${otherId}'`,
pass: true,
}
: {
message: () =>
`expected block id '${thisId}' to match other id '${otherId}'`,
pass: false,
};
},
});

Wyświetl plik

@ -46,6 +46,9 @@
"./client/tests/mock-fetch.js",
"./client/tests/mock-jquery.js"
],
"setupFilesAfterEnv": [
"./client/tests/utils.js"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"
]