Browse Source

Add custom jest matcher for block id duplicate tests

Joshua Munn 2 years ago
parent
commit
78fe000b19

+ 9 - 15
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],
     );
   });
 });

+ 5 - 5
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],
     );
   });
 });

+ 3 - 9
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],
     );
   });
 });

+ 6 - 9
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],
     );
   });
 });

+ 32 - 0
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,
+        };
+  },
+});

+ 3 - 0
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"
     ]