|
| 1 | +import type FS from 'node:fs' |
| 2 | + |
| 3 | +import { vi, describe, afterEach, beforeEach, it, expect } from 'vitest' |
| 4 | + |
| 5 | +// @ts-expect-error - no types for JS files |
| 6 | +import { handler } from '../realtimeHandler.js' |
| 7 | + |
| 8 | +const mocks = vi.hoisted(() => ({ |
| 9 | + realtimeTs: '', |
| 10 | + serverTs: '', |
| 11 | + writtenFiles: {} as Record<string, string>, |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('fs-extra', async () => { |
| 15 | + const fs = await vi.importActual<typeof FS>('fs-extra') |
| 16 | + |
| 17 | + return { |
| 18 | + default: { |
| 19 | + existsSync: (filePath: string) => { |
| 20 | + if (filePath === 'realtime.ts') { |
| 21 | + return !!mocks.realtimeTs |
| 22 | + } |
| 23 | + |
| 24 | + if (filePath === 'server.ts') { |
| 25 | + return !!mocks.serverTs |
| 26 | + } |
| 27 | + |
| 28 | + return !filePath.includes('foobar') |
| 29 | + }, |
| 30 | + mkdirSync: () => {}, |
| 31 | + readFileSync: (filePath: string) => { |
| 32 | + if (filePath.endsWith('blank.ts.template')) { |
| 33 | + return fs.readFileSync(filePath) |
| 34 | + } |
| 35 | + |
| 36 | + return '' |
| 37 | + }, |
| 38 | + writeFileSync: (dst: string, content: string) => { |
| 39 | + // Use `/` on both Windows and Unix |
| 40 | + mocks.writtenFiles[dst.replaceAll(/\\/g, '/')] = content |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | +}) |
| 45 | + |
| 46 | +vi.mock('@cedarjs/internal/dist/generate/generate', () => ({ |
| 47 | + generate: vi.fn().mockResolvedValue('success'), |
| 48 | +})) |
| 49 | + |
| 50 | +vi.mock('@cedarjs/project-config', () => ({ |
| 51 | + getPaths: () => { |
| 52 | + return { |
| 53 | + base: '', |
| 54 | + api: { src: '', lib: '', graphql: '', services: '', subscriptions: '' }, |
| 55 | + web: { base: '', src: '' }, |
| 56 | + } |
| 57 | + }, |
| 58 | + getConfig: () => ({ experimental: { streamingSsr: { enabled: false } } }), |
| 59 | + resolveFile: (path: string) => path, |
| 60 | +})) |
| 61 | + |
| 62 | +beforeEach(() => { |
| 63 | + vi.spyOn(console, 'log').mockImplementation(() => {}) |
| 64 | + vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 65 | + vi.spyOn(process, 'exit').mockImplementation(() => void 0 as never) |
| 66 | + mocks.realtimeTs = 'export const realtime: RedwoodRealtimeOptions = {}' |
| 67 | + mocks.serverTs = 'export const serverFile: RedwoodServerFileOptions = {}' |
| 68 | +}) |
| 69 | + |
| 70 | +afterEach(() => { |
| 71 | + vi.mocked(console).log.mockRestore?.() |
| 72 | + vi.mocked(console).error.mockRestore?.() |
| 73 | + vi.mocked(process).exit.mockRestore?.() |
| 74 | + mocks.writtenFiles = {} |
| 75 | +}) |
| 76 | + |
| 77 | +describe('realtimeHandler', () => { |
| 78 | + it("Should print error and exit if server-file isn't setup", async () => { |
| 79 | + mocks.serverTs = '' |
| 80 | + |
| 81 | + await handler({ |
| 82 | + name: 'noRealtime', |
| 83 | + type: 'subscription', |
| 84 | + silent: true, |
| 85 | + }) |
| 86 | + |
| 87 | + expect(vi.mocked(console).error).toHaveBeenCalledWith( |
| 88 | + expect.stringMatching( |
| 89 | + 'CedarJS Realtime requires a serverful environment. Please run `yarn ' + |
| 90 | + 'cedarjs setup server-file` first.', |
| 91 | + ), |
| 92 | + ) |
| 93 | + expect(vi.mocked(process).exit).toHaveBeenCalledWith(1) |
| 94 | + }) |
| 95 | + |
| 96 | + it("Should print error and exit if Realtime isn't setup", async () => { |
| 97 | + mocks.realtimeTs = '' |
| 98 | + |
| 99 | + await handler({ |
| 100 | + name: 'noRealtime', |
| 101 | + type: 'subscription', |
| 102 | + silent: true, |
| 103 | + }) |
| 104 | + |
| 105 | + expect(vi.mocked(console).error).toHaveBeenCalledWith( |
| 106 | + expect.stringMatching( |
| 107 | + 'Adding realtime events requires that CedarJS Realtime is setup. ' + |
| 108 | + 'Please run `yarn cedarjs setup realtime` first.', |
| 109 | + ), |
| 110 | + ) |
| 111 | + expect(vi.mocked(process).exit).toHaveBeenCalledWith(1) |
| 112 | + }) |
| 113 | + |
| 114 | + it('should generate a pubSub subscription', async () => { |
| 115 | + await handler({ |
| 116 | + name: 'foobar', |
| 117 | + type: 'subscription', |
| 118 | + silent: true, |
| 119 | + }) |
| 120 | + |
| 121 | + expect(mocks.writtenFiles['foobar/foobar.ts']).toMatch( |
| 122 | + "pubSub.subscribe('Foobar', id)", |
| 123 | + ) |
| 124 | + }) |
| 125 | +}) |
0 commit comments