|
| 1 | +import { readFile } from 'fs/promises'; |
| 2 | + |
| 3 | +import { isGraphQLDisabled } from './is-graphql-disabled'; |
| 4 | + |
| 5 | +jest.mock('fs/promises'); |
| 6 | + |
| 7 | +describe('isGraphQLDisabled', () => { |
| 8 | + const mockReadFile = jest.mocked(readFile); |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + jest.clearAllMocks(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + jest.restoreAllMocks(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should return true when graphQL.disable is true (basic format)', async () => { |
| 19 | + const content = ` |
| 20 | + export default buildConfig({ |
| 21 | + graphQL: { disable: true } |
| 22 | + }) |
| 23 | + `; |
| 24 | + mockReadFile.mockResolvedValue(content); |
| 25 | + |
| 26 | + const result = await isGraphQLDisabled('/path/to/config.ts'); |
| 27 | + |
| 28 | + expect(result).toBe(true); |
| 29 | + expect(mockReadFile).toHaveBeenCalledWith('/path/to/config.ts', 'utf-8'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should return true when graphQL.disable is true (with trailing comma)', async () => { |
| 33 | + const content = ` |
| 34 | + export default buildConfig({ |
| 35 | + graphQL: { disable: true, } |
| 36 | + }) |
| 37 | + `; |
| 38 | + mockReadFile.mockResolvedValue(content); |
| 39 | + |
| 40 | + expect(await isGraphQLDisabled('/path/to/config.ts')).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return true when disable is first property', async () => { |
| 44 | + const content = ` |
| 45 | + export default buildConfig({ |
| 46 | + graphQL: { |
| 47 | + disable: true, |
| 48 | + schema: { /* ... */ } |
| 49 | + } |
| 50 | + }) |
| 51 | + `; |
| 52 | + mockReadFile.mockResolvedValue(content); |
| 53 | + |
| 54 | + expect(await isGraphQLDisabled('/path/to/config.ts')).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should return true when disable is middle property', async () => { |
| 58 | + const content = ` |
| 59 | + export default buildConfig({ |
| 60 | + graphQL: { |
| 61 | + schema: { /* ... */ }, |
| 62 | + disable: true, |
| 63 | + playground: false |
| 64 | + } |
| 65 | + }) |
| 66 | + `; |
| 67 | + mockReadFile.mockResolvedValue(content); |
| 68 | + |
| 69 | + expect(await isGraphQLDisabled('/path/to/config.ts')).toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should return true when disable is last property', async () => { |
| 73 | + const content = ` |
| 74 | + export default buildConfig({ |
| 75 | + graphQL: { |
| 76 | + schema: { /* ... */ }, |
| 77 | + disable: true |
| 78 | + } |
| 79 | + }) |
| 80 | + `; |
| 81 | + mockReadFile.mockResolvedValue(content); |
| 82 | + |
| 83 | + expect(await isGraphQLDisabled('/path/to/config.ts')).toBe(true); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should return true with no spaces around colons', async () => { |
| 87 | + const content = ` |
| 88 | + export default buildConfig({ |
| 89 | + graphQL:{disable:true} |
| 90 | + }) |
| 91 | + `; |
| 92 | + mockReadFile.mockResolvedValue(content); |
| 93 | + |
| 94 | + expect(await isGraphQLDisabled('/path/to/config.ts')).toBe(true); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should return true with multiple newlines and spaces', async () => { |
| 98 | + const content = ` |
| 99 | + export default buildConfig({ |
| 100 | + graphQL: { |
| 101 | +
|
| 102 | + disable: true |
| 103 | +
|
| 104 | + } |
| 105 | + }) |
| 106 | + `; |
| 107 | + mockReadFile.mockResolvedValue(content); |
| 108 | + |
| 109 | + expect(await isGraphQLDisabled('/path/to/config.ts')).toBe(true); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should return false when graphQL.disable is false', async () => { |
| 113 | + const content = ` |
| 114 | + export default buildConfig({ |
| 115 | + graphQL: { disable: false } |
| 116 | + }) |
| 117 | + `; |
| 118 | + mockReadFile.mockResolvedValue(content); |
| 119 | + |
| 120 | + expect(await isGraphQLDisabled('/path/to/config.ts')).toBe(false); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should return false when graphQL.disable is missing', async () => { |
| 124 | + const content = ` |
| 125 | + export default buildConfig({ |
| 126 | + graphQL: { schema: { /* ... */ } } |
| 127 | + }) |
| 128 | + `; |
| 129 | + mockReadFile.mockResolvedValue(content); |
| 130 | + |
| 131 | + expect(await isGraphQLDisabled('/path/to/config.ts')).toBe(false); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should return false when graphQL config is missing', async () => { |
| 135 | + const content = ` |
| 136 | + export default buildConfig({ |
| 137 | + collections: [] |
| 138 | + }) |
| 139 | + `; |
| 140 | + mockReadFile.mockResolvedValue(content); |
| 141 | + |
| 142 | + expect(await isGraphQLDisabled('/path/to/config.ts')).toBe(false); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should not match commented out disable', async () => { |
| 146 | + const content = ` |
| 147 | + export default buildConfig({ |
| 148 | + graphQL: { |
| 149 | + // disable: true |
| 150 | + schema: { /* ... */ } |
| 151 | + } |
| 152 | + }) |
| 153 | + `; |
| 154 | + mockReadFile.mockResolvedValue(content); |
| 155 | + |
| 156 | + expect(await isGraphQLDisabled('/path/to/config.ts')).toBe(false); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should return false when file read fails', async () => { |
| 160 | + const consoleWarnSpy = jest |
| 161 | + .spyOn(console, 'warn') |
| 162 | + .mockImplementation(() => { |
| 163 | + // Suppress console output during tests |
| 164 | + }); |
| 165 | + mockReadFile.mockRejectedValue(new Error('File not found')); |
| 166 | + |
| 167 | + const result = await isGraphQLDisabled('/path/to/config.ts'); |
| 168 | + |
| 169 | + expect(result).toBe(false); |
| 170 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 171 | + expect.stringContaining('Failed to check GraphQL status'), |
| 172 | + 'File not found' |
| 173 | + ); |
| 174 | + |
| 175 | + consoleWarnSpy.mockRestore(); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should return false when file read throws non-Error', async () => { |
| 179 | + const consoleWarnSpy = jest |
| 180 | + .spyOn(console, 'warn') |
| 181 | + .mockImplementation(() => { |
| 182 | + // Suppress console output during tests |
| 183 | + }); |
| 184 | + mockReadFile.mockRejectedValue('Some string error'); |
| 185 | + |
| 186 | + const result = await isGraphQLDisabled('/path/to/config.ts'); |
| 187 | + |
| 188 | + expect(result).toBe(false); |
| 189 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 190 | + expect.stringContaining('Failed to check GraphQL status'), |
| 191 | + 'Some string error' |
| 192 | + ); |
| 193 | + |
| 194 | + consoleWarnSpy.mockRestore(); |
| 195 | + }); |
| 196 | + |
| 197 | + it('should handle empty file', async () => { |
| 198 | + mockReadFile.mockResolvedValue(''); |
| 199 | + |
| 200 | + expect(await isGraphQLDisabled('/path/to/config.ts')).toBe(false); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should handle malformed content gracefully', async () => { |
| 204 | + mockReadFile.mockResolvedValue('{'); |
| 205 | + |
| 206 | + expect(await isGraphQLDisabled('/path/to/config.ts')).toBe(false); |
| 207 | + }); |
| 208 | +}); |
0 commit comments