Quellcode durchsuchen

Simplify StreamBlock duplication tests, add StructBlock dup tests

Joshua Munn vor 2 Jahren
Ursprung
Commit
94d6ca055f

+ 17 - 49
client/src/components/StreamField/blocks/StreamBlock.test.js

@@ -337,11 +337,11 @@ describe('telepath: wagtail.blocks.StreamBlock', () => {
   });
 });
 
-describe('telepath: wagtail.blocks.StreamBlock with nested stream blocks', () => {
+describe('telepath: wagtail.blocks.StreamBlock with nested stream block', () => {
   let boundBlock;
 
   beforeEach(() => {
-    // Define a test block
+    // Define a test block - StreamBlock[StreamBlock[FieldBlock]]
     const innerStreamDef = new StreamBlockDefinition(
       'inner_stream',
       [
@@ -385,25 +385,7 @@ describe('telepath: wagtail.blocks.StreamBlock with nested stream blocks', () =>
 
     const blockDef = new StreamBlockDefinition(
       '',
-      [
-        [
-          '',
-          [
-            new StructBlockDefinition(
-              'struct_with_inner_stream',
-              [innerStreamDef],
-              {
-                label: 'Struct with inner stream',
-                required: false,
-                icon: 'placeholder',
-                classname: 'struct-block',
-                helpText: '',
-                helpIcon: '',
-              },
-            ),
-          ],
-        ],
-      ],
+      [['', [innerStreamDef]]],
       {},
       {
         label: '',
@@ -427,40 +409,26 @@ describe('telepath: wagtail.blocks.StreamBlock with nested stream blocks', () =>
 
     // Render it
     document.body.innerHTML = '<div id="placeholder"></div>';
-    boundBlock = blockDef.render($('#placeholder'), 'the-prefix', []);
-  });
-
-  test('duplicateBlock does not duplicate block ids', () => {
-    // Insert an instance of our struct block
-    boundBlock.insert(
-      {
-        type: 'struct_with_inner_stream',
-        value: { inner_stream: [] },
-        id: 'struct-1',
-      },
-      0,
-    );
-
-    // Insert a block into its nested stream field
-    boundBlock.children[0].block.childBlocks.inner_stream.insert(
+    boundBlock = blockDef.render($('#placeholder'), 'the-prefix', [
       {
-        type: 'test_block_a',
-        value: 'hello',
-        id: 'inner-id-1',
+        type: 'inner_stream',
+        value: [{ type: 'test_block_a', value: 'hello', id: 'inner-block-1' }],
+        id: 'nested-stream-1',
       },
-      0,
-    );
+    ]);
+  });
 
-    // Duplicate the struct block (outermost) instance
+  test('duplicateBlock does not duplicate block ids', () => {
     boundBlock.children[0].duplicate();
+    const duplicatedStreamChild = boundBlock.children[1];
+    const originalStreamChild = boundBlock.children[0];
 
-    expect(boundBlock.children[1].id).not.toBeNull();
-    expect(boundBlock.children[1].id).not.toEqual(boundBlock.children[0].id);
+    expect(duplicatedStreamChild.id).not.toBeNull();
+    expect(duplicatedStreamChild.id).not.toBeUndefined();
+    expect(duplicatedStreamChild.id).not.toEqual(originalStreamChild.id);
 
-    expect(
-      boundBlock.children[1].block.childBlocks.inner_stream.children[0].id,
-    ).not.toEqual(
-      boundBlock.children[0].block.childBlocks.inner_stream.children[0].id,
+    expect(duplicatedStreamChild.block.children[0].id).not.toEqual(
+      originalStreamChild.block.children[0].id,
     );
   });
 });

+ 137 - 0
client/src/components/StreamField/blocks/StructBlock.test.js

@@ -1,5 +1,6 @@
 import $ from 'jquery';
 import { FieldBlockDefinition } from './FieldBlock';
+import { StreamBlockDefinition } from './StreamBlock';
 import {
   StructBlockDefinition,
   StructBlockValidationError,
@@ -307,3 +308,139 @@ describe('telepath: wagtail.blocks.StructBlock with formTemplate', () => {
     );
   });
 });
+
+describe('telepath: wagtail.blocks.StructBlock in stream block', () => {
+  let boundBlock;
+
+  beforeEach(() => {
+    // Setup test blocks - StreamBlock[StructBlock[StreamBlock[FieldBlock], FieldBlock]]
+    const innerStreamDef = new StreamBlockDefinition(
+      'inner_stream',
+      [
+        [
+          '',
+          [
+            new FieldBlockDefinition(
+              'test_block_a',
+              new DummyWidgetDefinition('Block A Widget'),
+              {
+                label: 'Test Block A',
+                required: false,
+                icon: 'pilcrow',
+                classname:
+                  'w-field w-field--char_field w-field--admin_auto_height_text_input',
+              },
+            ),
+          ],
+        ],
+      ],
+      {},
+      {
+        label: 'Inner Stream',
+        required: false,
+        icon: 'placeholder',
+        classname: null,
+        helpText: '',
+        helpIcon: '',
+        maxNum: null,
+        minNum: null,
+        blockCounts: {},
+        strings: {
+          MOVE_UP: 'Move up',
+          MOVE_DOWN: 'Move down',
+          DELETE: 'Delete',
+          DUPLICATE: 'Duplicate',
+          ADD: 'Add',
+        },
+      },
+    );
+
+    const structBlockDef = new StructBlockDefinition(
+      'struct_block',
+      [
+        innerStreamDef,
+        new FieldBlockDefinition(
+          'test_block_b',
+          new DummyWidgetDefinition('Block A Widget'),
+          {
+            label: 'Test Block B',
+            required: false,
+            icon: 'pilcrow',
+            classname: '',
+          },
+        ),
+      ],
+      {
+        label: 'Heading block',
+        required: false,
+        icon: 'title',
+        classname: 'struct-block',
+        helpText: 'use <strong>lots</strong> of these',
+        helpIcon: '<div class="icon-help">?</div>',
+      },
+    );
+
+    const blockDef = new StreamBlockDefinition(
+      '',
+      [['', [structBlockDef]]],
+      {},
+      {
+        label: '',
+        required: true,
+        icon: 'placeholder',
+        classname: null,
+        helpText: 'use <strong>plenty</strong> of these',
+        helpIcon: '<div class="icon-help">?</div>',
+        maxNum: null,
+        minNum: null,
+        blockCounts: {},
+        strings: {
+          MOVE_UP: 'Move up',
+          MOVE_DOWN: 'Move down',
+          DELETE: 'Delete',
+          DUPLICATE: 'Duplicate',
+          ADD: 'Add',
+        },
+      },
+    );
+
+    // Render it
+    document.body.innerHTML = '<div id="placeholder"></div>';
+    boundBlock = blockDef.render($('#placeholder'), 'the-prefix', [
+      {
+        type: 'struct_block',
+        id: 'struct-block-1',
+        value: {
+          inner_stream: [
+            { type: 'test_block_a', id: 'very-nested-1', value: 'foobar' },
+          ],
+          test_block_b: 'hello, world',
+        },
+      },
+    ]);
+  });
+
+  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);
+
+    const duplicatedStreamBlockInStruct =
+      duplicatedStreamChild.block.childBlocks.inner_stream;
+    const originalStreamBlockInStruct =
+      originalStreamChild.block.childBlocks.inner_stream;
+    testIds(
+      originalStreamBlockInStruct.children[0],
+      duplicatedStreamBlockInStruct.children[0],
+    );
+  });
+});