|
| 1 | +import { findDuplicateActionIds } from "../src/helpers/find-duplicate-action-ids"; |
| 2 | + |
| 3 | +const button = (action_id?: string) => ({ |
| 4 | + type: "button", |
| 5 | + text: { type: "plain_text", text: "Go", emoji: true }, |
| 6 | + ...(action_id === undefined ? {} : { action_id }), |
| 7 | +}); |
| 8 | + |
| 9 | +describe("findDuplicateActionIds", () => { |
| 10 | + it("returns no errors when action_ids within a block are unique", () => { |
| 11 | + expect(findDuplicateActionIds([{ type: "actions", elements: [button("a"), button("b")] }])).toEqual([]); |
| 12 | + }); |
| 13 | + |
| 14 | + it("returns no errors when elements have no action_id", () => { |
| 15 | + expect(findDuplicateActionIds([{ type: "actions", elements: [button(), button()] }])).toEqual([]); |
| 16 | + }); |
| 17 | + |
| 18 | + it("flags a duplicate action_id in an actions block with both positions", () => { |
| 19 | + expect(findDuplicateActionIds([{ type: "actions", elements: [button("123"), button("123")] }])).toEqual([ |
| 20 | + "blocks[0].elements[1].action_id must be unique within the block — '123' appears at elements[0] and elements[1]", |
| 21 | + ]); |
| 22 | + }); |
| 23 | + |
| 24 | + it("flags each repeat of the same action_id independently", () => { |
| 25 | + const errors = findDuplicateActionIds([ |
| 26 | + { type: "actions", elements: [button("a"), button("b"), button("a"), button("a")] }, |
| 27 | + ]); |
| 28 | + expect(errors).toHaveLength(2); |
| 29 | + expect(errors[0]).toContain("'a' appears at elements[0] and elements[2]"); |
| 30 | + expect(errors[1]).toContain("'a' appears at elements[0] and elements[3]"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("allows the same action_id in two different blocks (state.values is keyed by block_id first)", () => { |
| 34 | + expect( |
| 35 | + findDuplicateActionIds([ |
| 36 | + { type: "actions", block_id: "one", elements: [button("a")] }, |
| 37 | + { type: "actions", block_id: "two", elements: [button("a")] }, |
| 38 | + { type: "section", text: { type: "mrkdwn", text: "hi" }, accessory: button("a") }, |
| 39 | + ]), |
| 40 | + ).toEqual([]); |
| 41 | + }); |
| 42 | + |
| 43 | + it("flags duplicates in a context_actions block", () => { |
| 44 | + const errors = findDuplicateActionIds([ |
| 45 | + { |
| 46 | + type: "context_actions", |
| 47 | + elements: [ |
| 48 | + { type: "icon_button", icon: "bolt", action_id: "x" }, |
| 49 | + { type: "icon_button", icon: "bolt", action_id: "x" }, |
| 50 | + ], |
| 51 | + }, |
| 52 | + ]); |
| 53 | + expect(errors).toEqual([ |
| 54 | + "blocks[0].elements[1].action_id must be unique within the block — 'x' appears at elements[0] and elements[1]", |
| 55 | + ]); |
| 56 | + }); |
| 57 | + |
| 58 | + it("flags duplicates across a card block's actions", () => { |
| 59 | + expect(findDuplicateActionIds([{ type: "card", actions: [button("go"), button("go")] }])).toEqual([ |
| 60 | + "blocks[0].actions[1].action_id must be unique within the block — 'go' appears at actions[0] and actions[1]", |
| 61 | + ]); |
| 62 | + }); |
| 63 | + |
| 64 | + it("scopes carousel cards separately and reports the offending card's path", () => { |
| 65 | + const errors = findDuplicateActionIds([ |
| 66 | + { |
| 67 | + type: "carousel", |
| 68 | + elements: [ |
| 69 | + { type: "card", actions: [button("go")] }, |
| 70 | + { type: "card", actions: [button("go"), button("go")] }, |
| 71 | + ], |
| 72 | + }, |
| 73 | + ]); |
| 74 | + expect(errors).toEqual([ |
| 75 | + "blocks[0].elements[1].actions[1].action_id must be unique within the block — 'go' appears at actions[0] and actions[1]", |
| 76 | + ]); |
| 77 | + }); |
| 78 | + |
| 79 | + it("scopes container child blocks separately", () => { |
| 80 | + const errors = findDuplicateActionIds([ |
| 81 | + { |
| 82 | + type: "container", |
| 83 | + child_blocks: [ |
| 84 | + { type: "actions", elements: [button("go")] }, |
| 85 | + { type: "actions", elements: [button("go"), button("go")] }, |
| 86 | + ], |
| 87 | + }, |
| 88 | + ]); |
| 89 | + expect(errors).toEqual([ |
| 90 | + "blocks[0].child_blocks[1].elements[1].action_id must be unique within the block — 'go' appears at elements[0] and elements[1]", |
| 91 | + ]); |
| 92 | + }); |
| 93 | + |
| 94 | + it("shares one namespace across an input block's element and any sibling keys", () => { |
| 95 | + expect( |
| 96 | + findDuplicateActionIds([ |
| 97 | + { |
| 98 | + type: "input", |
| 99 | + label: { type: "plain_text", text: "Name" }, |
| 100 | + element: { type: "plain_text_input", action_id: "a" }, |
| 101 | + }, |
| 102 | + ]), |
| 103 | + ).toEqual([]); |
| 104 | + }); |
| 105 | + |
| 106 | + it("ignores non-string action_ids", () => { |
| 107 | + expect( |
| 108 | + findDuplicateActionIds([{ type: "actions", elements: [{ action_id: 1 }, { action_id: 1 }, null, "nope"] }]), |
| 109 | + ).toEqual([]); |
| 110 | + }); |
| 111 | + |
| 112 | + it("accepts an empty array", () => { |
| 113 | + expect(findDuplicateActionIds([])).toEqual([]); |
| 114 | + }); |
| 115 | + |
| 116 | + it("terminates on pathologically nested containers", () => { |
| 117 | + let block: unknown = { type: "actions", elements: [button("a"), button("a")] }; |
| 118 | + for (let i = 0; i < 200; i++) { |
| 119 | + block = { type: "container", child_blocks: [block] }; |
| 120 | + } |
| 121 | + expect(findDuplicateActionIds([block])).toEqual([]); |
| 122 | + }); |
| 123 | +}); |
0 commit comments