|
| 1 | +import {expect} from 'chai'; |
| 2 | +import {before, describe, it} from 'mocha'; |
| 3 | +import {selectDashProps} from '../src/wrapper/selectors'; |
| 4 | + |
| 5 | +const leaf = {namespace: 'test_ns', type: 'Leaf', props: {}}; |
| 6 | +const flagged = children => ({ |
| 7 | + namespace: 'test_ns', |
| 8 | + type: 'Listens', |
| 9 | + props: {children} |
| 10 | +}); |
| 11 | + |
| 12 | +// The selector remembers the hashes it has seen in a module-level record, so |
| 13 | +// each test uses its own child index to stay independent of the others. |
| 14 | +const stateFor = (index, layoutHashes) => { |
| 15 | + const children = []; |
| 16 | + children[index] = flagged([leaf]); |
| 17 | + return {layout: {props: {children}}, layoutHashes}; |
| 18 | +}; |
| 19 | + |
| 20 | +const select = (index, layoutHashes) => { |
| 21 | + const path = ['props', 'children', index]; |
| 22 | + const [, , hash, changedProps, renderType] = selectDashProps(path)( |
| 23 | + stateFor(index, layoutHashes) |
| 24 | + ); |
| 25 | + return {hash, changedProps, renderType}; |
| 26 | +}; |
| 27 | + |
| 28 | +describe('selectDashProps with dashChildrenUpdate', () => { |
| 29 | + before(() => { |
| 30 | + window.test_ns = {Listens: {dashChildrenUpdate: true}, Leaf: {}}; |
| 31 | + }); |
| 32 | + |
| 33 | + it('sums its own hash and those of its first level children', () => { |
| 34 | + const self = 'props,children,1'; |
| 35 | + const {hash} = select(1, { |
| 36 | + [self]: {hash: 1}, |
| 37 | + [`${self},props,children`]: {hash: 10}, |
| 38 | + [`${self},props,children,0`]: {hash: 100} |
| 39 | + }); |
| 40 | + expect(hash).to.equal(111); |
| 41 | + }); |
| 42 | + |
| 43 | + it('ignores deeper descendants and unrelated paths', () => { |
| 44 | + const self = 'props,children,2'; |
| 45 | + const {hash} = select(2, { |
| 46 | + [self]: {hash: 1}, |
| 47 | + [`${self},props,children,0,props,children`]: {hash: 10}, |
| 48 | + 'props,children,3': {hash: 100}, |
| 49 | + props: {hash: 1000} |
| 50 | + }); |
| 51 | + expect(hash).to.equal(1); |
| 52 | + }); |
| 53 | + |
| 54 | + it('does not mistake a sibling with the same prefix for a child', () => { |
| 55 | + const self = 'props,children,4'; |
| 56 | + const {hash, changedProps} = select(4, { |
| 57 | + [self]: {hash: 1}, |
| 58 | + 'props,children,40': {hash: 10}, |
| 59 | + 'props,children,40,props,children': {hash: 100} |
| 60 | + }); |
| 61 | + expect(hash).to.equal(1); |
| 62 | + expect(changedProps).to.deep.equal({}); |
| 63 | + }); |
| 64 | + |
| 65 | + it('reports the prop of an updated child only once', () => { |
| 66 | + const hashes = {'props,children,5,props,children': {hash: 1}}; |
| 67 | + expect(select(5, hashes)).to.deep.equal({ |
| 68 | + hash: 1, |
| 69 | + changedProps: {children: true}, |
| 70 | + renderType: 'components' |
| 71 | + }); |
| 72 | + expect(select(5, hashes)).to.deep.equal({ |
| 73 | + hash: 1, |
| 74 | + changedProps: {}, |
| 75 | + renderType: undefined |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('reports its own changed props and render type', () => { |
| 80 | + const {hash, changedProps, renderType} = select(6, { |
| 81 | + 'props,children,6': { |
| 82 | + hash: 2, |
| 83 | + changedProps: {value: 1}, |
| 84 | + renderType: 'callback' |
| 85 | + } |
| 86 | + }); |
| 87 | + expect(hash).to.equal(2); |
| 88 | + expect(changedProps).to.deep.equal({value: 1}); |
| 89 | + expect(renderType).to.equal('callback'); |
| 90 | + }); |
| 91 | +}); |
0 commit comments