|
| 1 | +import { writeFile } from 'fs/promises' |
| 2 | +import { glob } from 'glob' |
| 3 | +import { buildPropsData } from '../buildPropsData' |
| 4 | +import { getConfig } from '../getConfig' |
| 5 | +import { tsDocgen } from '../tsDocGen' |
| 6 | + |
| 7 | +jest.mock('fs/promises') |
| 8 | +jest.mock('glob') |
| 9 | +jest.mock('../getConfig') |
| 10 | +jest.mock('../tsDocGen') |
| 11 | + |
| 12 | +// suppress console logs so that it doesn't clutter the test output |
| 13 | +jest.spyOn(console, 'log').mockImplementation(() => {}) |
| 14 | + |
| 15 | +const validConfigResponse = { |
| 16 | + propsGlobs: [ |
| 17 | + { |
| 18 | + include: ['**/include/files/*', '**/include/other/files/*'], |
| 19 | + exclude: ['**/exclude/files/*'], |
| 20 | + }, |
| 21 | + { include: ['**/one/more/include/*'], exclude: [] }, |
| 22 | + ], |
| 23 | +} |
| 24 | + |
| 25 | +const sharedPropData = [ |
| 26 | + { |
| 27 | + name: 'firstProp', |
| 28 | + type: 'firstPropType', |
| 29 | + description: 'this is the first prop', |
| 30 | + required: false, |
| 31 | + defaultValue: 'firstDefaultValue', |
| 32 | + hide: false, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: 'secondProp', |
| 36 | + type: 'secondPropType', |
| 37 | + description: 'this is the second prop', |
| 38 | + required: false, |
| 39 | + defaultValue: 'secondDefaultValue', |
| 40 | + hide: false, |
| 41 | + }, |
| 42 | +] |
| 43 | + |
| 44 | +const validTsDocGenResponseOne = [ |
| 45 | + { |
| 46 | + name: 'ComponentOne', |
| 47 | + description: 'This is the first component', |
| 48 | + props: sharedPropData, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: 'ComponentTwo', |
| 52 | + description: 'This is the second component', |
| 53 | + props: sharedPropData, |
| 54 | + }, |
| 55 | +] |
| 56 | + |
| 57 | +const validTsDocGenResponseTwo = [ |
| 58 | + { |
| 59 | + name: 'ComponentThree', |
| 60 | + description: 'This is the third component', |
| 61 | + props: sharedPropData, |
| 62 | + }, |
| 63 | +] |
| 64 | + |
| 65 | +const propsData = { |
| 66 | + ComponentOne: { |
| 67 | + name: 'ComponentOne', |
| 68 | + description: 'This is the first component', |
| 69 | + props: sharedPropData, |
| 70 | + }, |
| 71 | + ComponentTwo: { |
| 72 | + name: 'ComponentTwo', |
| 73 | + description: 'This is the second component', |
| 74 | + props: sharedPropData, |
| 75 | + }, |
| 76 | + ComponentThree: { |
| 77 | + name: 'ComponentThree', |
| 78 | + description: 'This is the third component', |
| 79 | + props: sharedPropData, |
| 80 | + }, |
| 81 | +} |
| 82 | + |
| 83 | +it('should call getConfig with the passed config file location', async () => { |
| 84 | + await buildPropsData('/root/', '/astro/', '/config', false) |
| 85 | + |
| 86 | + expect(getConfig).toHaveBeenCalledWith('/config') |
| 87 | +}) |
| 88 | + |
| 89 | +it('should not proceed if config is not found', async () => { |
| 90 | + ;(getConfig as jest.Mock).mockResolvedValue(undefined) |
| 91 | + |
| 92 | + await buildPropsData('/root/', '/astro/', '/config', false) |
| 93 | + |
| 94 | + expect(writeFile).not.toHaveBeenCalled() |
| 95 | +}) |
| 96 | + |
| 97 | +it('should send an error to the console if the config file does not have a propsGlobs entry', async () => { |
| 98 | + ;(getConfig as jest.Mock).mockResolvedValue({ foo: 'bar' }) |
| 99 | + |
| 100 | + const mockConsoleError = jest.fn() |
| 101 | + jest.spyOn(console, 'error').mockImplementation(mockConsoleError) |
| 102 | + |
| 103 | + await buildPropsData('/root/', '/astro/', '/config', false) |
| 104 | + |
| 105 | + expect(mockConsoleError).toHaveBeenCalledWith('No props data found in config') |
| 106 | + expect(writeFile).not.toHaveBeenCalled() |
| 107 | +}) |
| 108 | + |
| 109 | +it('should call glob with the propGlobs in the config file and the cwd set to the root dir', async () => { |
| 110 | + ;(getConfig as jest.Mock).mockResolvedValue(validConfigResponse) |
| 111 | + ;(glob as unknown as jest.Mock).mockResolvedValue(['files/one', 'files/two']) |
| 112 | + ;(tsDocgen as jest.Mock).mockResolvedValue(validTsDocGenResponseOne) |
| 113 | + |
| 114 | + await buildPropsData('/root/', '/astro/', '/config', false) |
| 115 | + |
| 116 | + expect(glob).toHaveBeenNthCalledWith( |
| 117 | + 1, |
| 118 | + ['**/include/files/*', '**/include/other/files/*'], |
| 119 | + { cwd: '/root/', ignore: ['**/exclude/files/*'] }, |
| 120 | + ) |
| 121 | + expect(glob).toHaveBeenNthCalledWith(2, ['**/one/more/include/*'], { |
| 122 | + cwd: '/root/', |
| 123 | + ignore: [], |
| 124 | + }) |
| 125 | + expect(glob).toHaveBeenCalledTimes(2) |
| 126 | +}) |
| 127 | + |
| 128 | +it('should call tsDocGen with each file that glob returns', async () => { |
| 129 | + ;(getConfig as jest.Mock).mockResolvedValue(validConfigResponse) |
| 130 | + ;(glob as unknown as jest.Mock).mockResolvedValueOnce([ |
| 131 | + 'files/one', |
| 132 | + 'files/two', |
| 133 | + ]) |
| 134 | + ;(glob as unknown as jest.Mock).mockResolvedValueOnce([ |
| 135 | + 'files/three', |
| 136 | + 'files/four', |
| 137 | + ]) |
| 138 | + ;(tsDocgen as jest.Mock).mockReset() |
| 139 | + ;(tsDocgen as jest.Mock).mockResolvedValue(validTsDocGenResponseOne) |
| 140 | + |
| 141 | + await buildPropsData('/root/', '/astro/', '/config', false) |
| 142 | + |
| 143 | + expect(tsDocgen).toHaveBeenNthCalledWith(1, 'files/one') |
| 144 | + expect(tsDocgen).toHaveBeenNthCalledWith(2, 'files/two') |
| 145 | + expect(tsDocgen).toHaveBeenNthCalledWith(3, 'files/three') |
| 146 | + expect(tsDocgen).toHaveBeenNthCalledWith(4, 'files/four') |
| 147 | + expect(tsDocgen).toHaveBeenCalledTimes(4) |
| 148 | +}) |
| 149 | + |
| 150 | +it('should call writeFile with the returned prop data in JSON form', async () => { |
| 151 | + ;(getConfig as jest.Mock).mockResolvedValue(validConfigResponse) |
| 152 | + ;(glob as unknown as jest.Mock).mockResolvedValueOnce(['files/one']) |
| 153 | + ;(glob as unknown as jest.Mock).mockResolvedValueOnce(['files/two']) |
| 154 | + ;(tsDocgen as jest.Mock).mockResolvedValueOnce(validTsDocGenResponseOne) |
| 155 | + ;(tsDocgen as jest.Mock).mockResolvedValueOnce(validTsDocGenResponseTwo) |
| 156 | + ;(writeFile as jest.Mock).mockReset() |
| 157 | + |
| 158 | + await buildPropsData('/root/', '/astro/', '/config', false) |
| 159 | + |
| 160 | + expect(writeFile).toHaveBeenCalledWith( |
| 161 | + '/astro/dist/props.json', |
| 162 | + JSON.stringify(propsData), |
| 163 | + ) |
| 164 | +}) |
0 commit comments