|
| 1 | +import { Tree, TreeSvgProps } from '@nivo/tree' |
| 2 | +import { before } from 'lodash' |
| 3 | + |
| 4 | +interface Datum { |
| 5 | + id: string |
| 6 | + children?: Datum[] |
| 7 | +} |
| 8 | + |
| 9 | +const sampleData: Datum = { |
| 10 | + id: 'A', |
| 11 | + children: [ |
| 12 | + { id: '0' }, |
| 13 | + { |
| 14 | + id: '1', |
| 15 | + children: [{ id: 'A' }, { id: 'B' }], |
| 16 | + }, |
| 17 | + { id: '2' }, |
| 18 | + ], |
| 19 | +} |
| 20 | + |
| 21 | +const defaultProps: Pick< |
| 22 | + TreeSvgProps<Datum>, |
| 23 | + | 'data' |
| 24 | + | 'width' |
| 25 | + | 'height' |
| 26 | + | 'margin' |
| 27 | + | 'nodeSize' |
| 28 | + | 'activeNodeSize' |
| 29 | + | 'inactiveNodeSize' |
| 30 | + | 'linkThickness' |
| 31 | + | 'activeLinkThickness' |
| 32 | + | 'inactiveLinkThickness' |
| 33 | + | 'animate' |
| 34 | +> = { |
| 35 | + data: sampleData, |
| 36 | + width: 640, |
| 37 | + height: 640, |
| 38 | + margin: { |
| 39 | + top: 20, |
| 40 | + right: 20, |
| 41 | + bottom: 20, |
| 42 | + left: 20, |
| 43 | + }, |
| 44 | + nodeSize: 12, |
| 45 | + activeNodeSize: 24, |
| 46 | + inactiveNodeSize: 8, |
| 47 | + linkThickness: 2, |
| 48 | + activeLinkThickness: 12, |
| 49 | + inactiveLinkThickness: 1, |
| 50 | + animate: false, |
| 51 | +} |
| 52 | + |
| 53 | +describe('<Tree />', () => { |
| 54 | + beforeEach(() => { |
| 55 | + cy.viewport( |
| 56 | + defaultProps.margin.left + defaultProps.width + defaultProps.margin.right, |
| 57 | + defaultProps.margin.top + defaultProps.height + defaultProps.margin.bottom |
| 58 | + ) |
| 59 | + }) |
| 60 | + |
| 61 | + it('should render a tree graph', () => { |
| 62 | + cy.mount(<Tree<Datum> {...defaultProps} />) |
| 63 | + |
| 64 | + cy.get('[data-testid="node.A"]').should('exist') |
| 65 | + cy.get('[data-testid="node.A.0"]').should('exist') |
| 66 | + cy.get('[data-testid="node.A.1"]').should('exist') |
| 67 | + cy.get('[data-testid="node.A.1.A"]').should('exist') |
| 68 | + cy.get('[data-testid="node.A.1.B"]').should('exist') |
| 69 | + cy.get('[data-testid="node.A.2"]').should('exist') |
| 70 | + }) |
| 71 | + |
| 72 | + it('should highlight ancestor nodes and links', () => { |
| 73 | + cy.mount( |
| 74 | + <Tree<Datum> |
| 75 | + {...defaultProps} |
| 76 | + useMesh={false} |
| 77 | + highlightAncestorNodes={true} |
| 78 | + highlightAncestorLinks={true} |
| 79 | + /> |
| 80 | + ) |
| 81 | + |
| 82 | + const expectations = [ |
| 83 | + { uid: 'node.A', nodes: ['node.A'], links: [] }, |
| 84 | + { uid: 'node.A.0', nodes: ['node.A', 'node.A.0'], links: ['link.A:A.0'] }, |
| 85 | + { uid: 'node.A.1', nodes: ['node.A', 'node.A.1'], links: ['link.A:A.1'] }, |
| 86 | + { |
| 87 | + uid: 'node.A.1.A', |
| 88 | + nodes: ['node.A', 'node.A.1', 'node.A.1.A'], |
| 89 | + links: ['link.A:A.1', 'link.A.1:A.1.A'], |
| 90 | + }, |
| 91 | + { |
| 92 | + uid: 'node.A.1.B', |
| 93 | + nodes: ['node.A', 'node.A.1', 'node.A.1.B'], |
| 94 | + links: ['link.A:A.1', 'link.A.1:A.1.B'], |
| 95 | + }, |
| 96 | + { uid: 'node.A.2', nodes: ['node.A', 'node.A.2'], links: ['link.A:A.2'] }, |
| 97 | + ] |
| 98 | + |
| 99 | + for (const expectation of expectations) { |
| 100 | + cy.get(`[data-testid="${expectation.uid}"]`).trigger('mouseover') |
| 101 | + cy.wait(100) |
| 102 | + |
| 103 | + cy.get('[data-testid^="node."]').each($node => { |
| 104 | + cy.wrap($node) |
| 105 | + .invoke('attr', 'data-testid') |
| 106 | + .then(testId => { |
| 107 | + const size = expectation.nodes.includes(testId!) |
| 108 | + ? defaultProps.activeNodeSize |
| 109 | + : defaultProps.inactiveNodeSize |
| 110 | + cy.wrap($node) |
| 111 | + .invoke('attr', 'r') |
| 112 | + .should('equal', `${size / 2}`) |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | + cy.get('[data-testid^="link."]').each($link => { |
| 117 | + cy.wrap($link) |
| 118 | + .invoke('attr', 'data-testid') |
| 119 | + .then(testId => { |
| 120 | + const thickness = expectation.links.includes(testId!) |
| 121 | + ? defaultProps.activeLinkThickness |
| 122 | + : defaultProps.inactiveLinkThickness |
| 123 | + cy.wrap($link) |
| 124 | + .invoke('attr', 'stroke-width') |
| 125 | + .should('equal', `${thickness}`) |
| 126 | + }) |
| 127 | + }) |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | + it('should highlight descendant nodes and links', () => { |
| 132 | + cy.mount( |
| 133 | + <Tree<Datum> |
| 134 | + {...defaultProps} |
| 135 | + useMesh={false} |
| 136 | + highlightAncestorNodes={false} |
| 137 | + highlightAncestorLinks={false} |
| 138 | + highlightDescendantNodes={true} |
| 139 | + highlightDescendantLinks={true} |
| 140 | + /> |
| 141 | + ) |
| 142 | + |
| 143 | + const expectations = [ |
| 144 | + { |
| 145 | + uid: 'node.A', |
| 146 | + nodes: ['node.A', 'node.A.0', 'node.A.1', 'node.A.1.A', 'node.A.1.B', 'node.A.2'], |
| 147 | + links: [], |
| 148 | + }, |
| 149 | + { uid: 'node.A.0', nodes: ['node.A.0'], links: [] }, |
| 150 | + { uid: 'node.A.1', nodes: ['node.A.1', 'node.A.1.A', 'node.A.1.B'], links: [] }, |
| 151 | + { uid: 'node.A.1.A', nodes: ['node.A.1.A'], links: [] }, |
| 152 | + { uid: 'node.A.1.B', nodes: ['node.A.1.B'], links: [] }, |
| 153 | + { uid: 'node.A.2', nodes: ['node.A.2'], links: [] }, |
| 154 | + ] |
| 155 | + |
| 156 | + for (const expectation of expectations) { |
| 157 | + cy.get(`[data-testid="${expectation.uid}"]`).trigger('mouseover') |
| 158 | + cy.wait(100) |
| 159 | + |
| 160 | + cy.get('[data-testid^="node."]').each($node => { |
| 161 | + cy.wrap($node) |
| 162 | + .invoke('attr', 'data-testid') |
| 163 | + .then(testId => { |
| 164 | + const size = expectation.nodes.includes(testId!) |
| 165 | + ? defaultProps.activeNodeSize |
| 166 | + : defaultProps.inactiveNodeSize |
| 167 | + cy.wrap($node) |
| 168 | + .invoke('attr', 'r') |
| 169 | + .should('equal', `${size / 2}`) |
| 170 | + }) |
| 171 | + }) |
| 172 | + } |
| 173 | + }) |
| 174 | +}) |
0 commit comments