|
| 1 | +import { createCollectionContent } from '../createCollectionContent' |
| 2 | +import { getConfig } from '../getConfig' |
| 3 | +import { writeFile } from 'fs/promises' |
| 4 | + |
| 5 | +jest.mock('../getConfig') |
| 6 | +jest.mock('fs/promises') |
| 7 | + |
| 8 | +// suppress console.log so that it doesn't clutter the test output |
| 9 | +jest.spyOn(console, 'log').mockImplementation(() => {}) |
| 10 | + |
| 11 | +it('should call getConfig with the passed config file location', async () => { |
| 12 | + await createCollectionContent('/foo/', 'bar', false) |
| 13 | + |
| 14 | + expect(getConfig).toHaveBeenCalledWith('bar') |
| 15 | +}) |
| 16 | + |
| 17 | +it('should not proceed if config is not found', async () => { |
| 18 | + ;(getConfig as jest.Mock).mockResolvedValue(undefined) |
| 19 | + |
| 20 | + await createCollectionContent('/foo/', 'bar', false) |
| 21 | + |
| 22 | + expect(writeFile).not.toHaveBeenCalled() |
| 23 | +}) |
| 24 | + |
| 25 | +it('should log error if content is not found in config', async () => { |
| 26 | + ;(getConfig as jest.Mock).mockResolvedValue({ foo: 'bar' }) |
| 27 | + |
| 28 | + const mockConsoleError = jest.fn() |
| 29 | + jest.spyOn(console, 'error').mockImplementation(mockConsoleError) |
| 30 | + |
| 31 | + await createCollectionContent('/foo/', 'bar', false) |
| 32 | + |
| 33 | + expect(mockConsoleError).toHaveBeenCalledWith('No content found in config') |
| 34 | + expect(writeFile).not.toHaveBeenCalled() |
| 35 | +}) |
| 36 | + |
| 37 | +it('should call writeFile with the expected file location and content without throwing any errors', async () => { |
| 38 | + ;(getConfig as jest.Mock).mockResolvedValue({ content: { key: 'value' } }) |
| 39 | + |
| 40 | + const mockConsoleError = jest.fn() |
| 41 | + jest.spyOn(console, 'error').mockImplementation(mockConsoleError) |
| 42 | + |
| 43 | + await createCollectionContent('/foo/', 'bar', false) |
| 44 | + |
| 45 | + expect(writeFile).toHaveBeenCalledWith( |
| 46 | + '/foo/src/content.ts', |
| 47 | + `export const content = ${JSON.stringify({ key: 'value' })}`, |
| 48 | + ) |
| 49 | + expect(mockConsoleError).not.toHaveBeenCalled() |
| 50 | +}) |
| 51 | + |
| 52 | +it('should log error if writeFile throws an error', async () => { |
| 53 | + ;(getConfig as jest.Mock).mockResolvedValue({ content: { key: 'value' } }) |
| 54 | + |
| 55 | + const mockConsoleError = jest.fn() |
| 56 | + jest.spyOn(console, 'error').mockImplementation(mockConsoleError) |
| 57 | + |
| 58 | + const error = new Error('error') |
| 59 | + ;(writeFile as jest.Mock).mockRejectedValue(error) |
| 60 | + |
| 61 | + await createCollectionContent('/foo/', 'bar', false) |
| 62 | + |
| 63 | + expect(mockConsoleError).toHaveBeenCalledWith( |
| 64 | + 'Error writing content file', |
| 65 | + error, |
| 66 | + ) |
| 67 | +}) |
| 68 | + |
| 69 | +it('should log that content file was created when run in verbose mode', async () => { |
| 70 | + const mockConsoleLog = jest.fn() |
| 71 | + jest.spyOn(console, 'log').mockImplementation(mockConsoleLog) |
| 72 | + |
| 73 | + await createCollectionContent('/foo/', 'bar', true) |
| 74 | + |
| 75 | + expect(mockConsoleLog).toHaveBeenCalledWith('Content file created') |
| 76 | +}) |
| 77 | + |
| 78 | +it('should not log that content file was created when not run in verbose mode', async () => { |
| 79 | + const mockConsoleLog = jest.fn() |
| 80 | + jest.spyOn(console, 'log').mockImplementation(mockConsoleLog) |
| 81 | + |
| 82 | + await createCollectionContent('/foo/', 'bar', false) |
| 83 | + |
| 84 | + expect(mockConsoleLog).not.toHaveBeenCalled() |
| 85 | +}) |
0 commit comments