From 78fe000b19f40ca334ea0143a4780eed797459ad Mon Sep 17 00:00:00 2001 From: Joshua Munn Date: Thu, 1 Sep 2022 17:35:44 +0100 Subject: [PATCH] Add custom jest matcher for block id duplicate tests --- .../StreamField/blocks/ListBlock.test.js | 24 ++++++-------- .../StreamField/blocks/StreamBlock.test.js | 10 +++--- .../StreamField/blocks/StructBlock.test.js | 12 ++----- .../typed_table_block.test.js | 15 ++++----- client/tests/utils.js | 32 +++++++++++++++++++ package.json | 3 ++ 6 files changed, 58 insertions(+), 38 deletions(-) create mode 100644 client/tests/utils.js diff --git a/client/src/components/StreamField/blocks/ListBlock.test.js b/client/src/components/StreamField/blocks/ListBlock.test.js index 74b57a0429..d82f162690 100644 --- a/client/src/components/StreamField/blocks/ListBlock.test.js +++ b/client/src/components/StreamField/blocks/ListBlock.test.js @@ -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], ); }); }); diff --git a/client/src/components/StreamField/blocks/StreamBlock.test.js b/client/src/components/StreamField/blocks/StreamBlock.test.js index 14ea626713..b89fd0b5df 100644 --- a/client/src/components/StreamField/blocks/StreamBlock.test.js +++ b/client/src/components/StreamField/blocks/StreamBlock.test.js @@ -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], ); }); }); diff --git a/client/src/components/StreamField/blocks/StructBlock.test.js b/client/src/components/StreamField/blocks/StructBlock.test.js index 6008d9fab8..e3bb6bffce 100644 --- a/client/src/components/StreamField/blocks/StructBlock.test.js +++ b/client/src/components/StreamField/blocks/StructBlock.test.js @@ -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], ); }); }); diff --git a/client/src/entrypoints/contrib/typed_table_block/typed_table_block.test.js b/client/src/entrypoints/contrib/typed_table_block/typed_table_block.test.js index 38dcfd55b8..8fc24280d1 100644 --- a/client/src/entrypoints/contrib/typed_table_block/typed_table_block.test.js +++ b/client/src/entrypoints/contrib/typed_table_block/typed_table_block.test.js @@ -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], ); }); }); diff --git a/client/tests/utils.js b/client/tests/utils.js new file mode 100644 index 0000000000..dc61d306b2 --- /dev/null +++ b/client/tests/utils.js @@ -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, + }; + }, +}); diff --git a/package.json b/package.json index 2142d42f01..62f1e63f28 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,9 @@ "./client/tests/mock-fetch.js", "./client/tests/mock-jquery.js" ], + "setupFilesAfterEnv": [ + "./client/tests/utils.js" + ], "snapshotSerializers": [ "enzyme-to-json/serializer" ]