|
| 1 | +import { render } from '@testing-library/react' |
| 2 | +import { describe, expect, it, vi } from 'vitest' |
| 3 | + |
| 4 | +import { |
| 5 | + dataModulesHorizontalLineNeighbours, |
| 6 | + dataModulesLeafNeighbours, |
| 7 | + dataModulesRoundedNeighbours, |
| 8 | + dataModulesVerticalLineNeighbours, |
| 9 | +} from '../test/data-modules-neightbours' |
| 10 | +import type { DataModulesStyle } from '../types/lib' |
| 11 | +import type { DataModulesNeighbours } from '../types/utils' |
| 12 | +import * as dataModulesUtils from '../utils/data-modules' |
| 13 | +import * as svgUtils from '../utils/svg' |
| 14 | +import { DataModules } from './data-modules' |
| 15 | + |
| 16 | +describe('DataModules', () => { |
| 17 | + const mockModules = [ |
| 18 | + [true, false, true], |
| 19 | + [false, true, false], |
| 20 | + [true, false, true], |
| 21 | + ] |
| 22 | + |
| 23 | + const getModuleNeighboursSpy = vi.spyOn(dataModulesUtils, 'getModuleNeighbours') |
| 24 | + |
| 25 | + const defaultProps = { |
| 26 | + modules: mockModules, |
| 27 | + margin: 2, |
| 28 | + } |
| 29 | + |
| 30 | + it('calls the correct shape function based on style prop', () => { |
| 31 | + const stylesToMethods = { |
| 32 | + square: 'square', |
| 33 | + circle: 'circle', |
| 34 | + diamond: 'diamond', |
| 35 | + heart: 'heart', |
| 36 | + star: 'star', |
| 37 | + } |
| 38 | + |
| 39 | + Object.entries(stylesToMethods).forEach(([style, method]) => { |
| 40 | + const utils = style === 'heart' || style === 'star' ? svgUtils : dataModulesUtils |
| 41 | + const spy = vi.spyOn(utils, method as keyof typeof utils) |
| 42 | + |
| 43 | + render( |
| 44 | + <DataModules settings={{ style: style as DataModulesStyle }} {...defaultProps} />, |
| 45 | + ) |
| 46 | + |
| 47 | + expect(spy).toHaveBeenCalled() |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + it.each(dataModulesRoundedNeighbours)( |
| 52 | + `with neighbours %j calls the correct shape function %s for style rounded`, |
| 53 | + (neighbours, method) => { |
| 54 | + getModuleNeighboursSpy.mockImplementation(() => neighbours as DataModulesNeighbours) |
| 55 | + const methodSpy = vi.spyOn( |
| 56 | + dataModulesUtils, |
| 57 | + method as keyof typeof dataModulesUtils, |
| 58 | + ) |
| 59 | + |
| 60 | + render(<DataModules settings={{ style: 'rounded' }} {...defaultProps} />) |
| 61 | + |
| 62 | + expect(methodSpy).toHaveBeenCalled() |
| 63 | + }, |
| 64 | + ) |
| 65 | + |
| 66 | + it.each(dataModulesLeafNeighbours)( |
| 67 | + `with neighbours %j calls the correct shape function %s for style leaf`, |
| 68 | + (neighbours, method) => { |
| 69 | + getModuleNeighboursSpy.mockImplementation(() => neighbours as DataModulesNeighbours) |
| 70 | + const methodSpy = vi.spyOn( |
| 71 | + dataModulesUtils, |
| 72 | + method as keyof typeof dataModulesUtils, |
| 73 | + ) |
| 74 | + |
| 75 | + render(<DataModules settings={{ style: 'leaf' }} {...defaultProps} />) |
| 76 | + |
| 77 | + expect(methodSpy).toHaveBeenCalled() |
| 78 | + }, |
| 79 | + ) |
| 80 | + |
| 81 | + it.each(dataModulesVerticalLineNeighbours)( |
| 82 | + `with neighbours %j calls the correct shape function %s for style leaf`, |
| 83 | + (neighbours, method) => { |
| 84 | + getModuleNeighboursSpy.mockImplementation(() => neighbours as DataModulesNeighbours) |
| 85 | + const methodSpy = vi.spyOn( |
| 86 | + dataModulesUtils, |
| 87 | + method as keyof typeof dataModulesUtils, |
| 88 | + ) |
| 89 | + |
| 90 | + render(<DataModules settings={{ style: 'vertical-line' }} {...defaultProps} />) |
| 91 | + |
| 92 | + expect(methodSpy).toHaveBeenCalled() |
| 93 | + }, |
| 94 | + ) |
| 95 | + |
| 96 | + it.each(dataModulesHorizontalLineNeighbours)( |
| 97 | + `with neighbours %j calls the correct shape function %s for style leaf`, |
| 98 | + (neighbours, method) => { |
| 99 | + getModuleNeighboursSpy.mockImplementation(() => neighbours as DataModulesNeighbours) |
| 100 | + const methodSpy = vi.spyOn( |
| 101 | + dataModulesUtils, |
| 102 | + method as keyof typeof dataModulesUtils, |
| 103 | + ) |
| 104 | + |
| 105 | + render(<DataModules settings={{ style: 'horizontal-line' }} {...defaultProps} />) |
| 106 | + |
| 107 | + expect(methodSpy).toHaveBeenCalled() |
| 108 | + }, |
| 109 | + ) |
| 110 | +}) |
0 commit comments