-
Notifications
You must be signed in to change notification settings - Fork 0
Added Testcases #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aakanksha994
wants to merge
4
commits into
main
Choose a base branch
from
testcases
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| module.exports = { | ||
| testEnvironment: 'jsdom', | ||
| setupFilesAfterEnv: ['<rootDir>/test-setup.ts'], | ||
| transform: { | ||
| '^.+\\.(ts|tsx)$': 'ts-jest', | ||
| }, | ||
| moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import type { Config } from 'jest'; | ||
|
|
||
| const config: Config = { | ||
| preset: 'ts-jest', | ||
| testEnvironment: 'node', | ||
| roots: ['<rootDir>/src'], | ||
| setupFilesAfterEnv: ['<rootDir>/../../test-setup.ts'], | ||
| moduleFileExtensions: ['ts', 'tsx', 'js'], | ||
| testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)'], | ||
| transform: { | ||
| '^.+\\.(ts|tsx)$': 'ts-jest', | ||
| }, | ||
| collectCoverage: true, | ||
| coverageDirectory: 'coverage', | ||
| verbose: true, | ||
| }; | ||
|
|
||
| export default config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { getHandler, pubSub } from './pubsub'; | ||
| import { | ||
| EventName, | ||
| type SubscriptionMessage, | ||
| type ConsumerFn, | ||
| type QueueConfig, | ||
| } from './types'; | ||
| import * as adapterModule from '../queue-adapters/adapter'; | ||
| import * as pubsubModule from './pubsub'; | ||
|
|
||
| describe('getHandler', () => { | ||
| it('returns sendMessage from QueueConfig', async () => { | ||
| const mockSendMessage = jest.fn(); | ||
| const mockGetQueue = jest | ||
| .spyOn(adapterModule, 'getQueue') | ||
| .mockResolvedValue({ | ||
| sendMessage: mockSendMessage, | ||
| sendMessages: jest.fn(), | ||
| }); | ||
|
|
||
| const queueConfig: QueueConfig<string> = { | ||
| type: 'bullmq', | ||
| options: { key: 'value' }, | ||
| }; | ||
|
|
||
| const handler = await getHandler(queueConfig); | ||
| expect(mockGetQueue).toHaveBeenCalledWith(queueConfig); | ||
| expect(handler).toBe(mockSendMessage); | ||
| }); | ||
|
|
||
| it('returns function directly if consumer is a ConsumerFn', async () => { | ||
| const mockConsumerFn: ConsumerFn = jest.fn(); | ||
| const handler = await getHandler(mockConsumerFn); | ||
| expect(handler).toBe(mockConsumerFn); | ||
| }); | ||
|
|
||
| it('returns null for unknown type', async () => { | ||
| const handler = await getHandler('unknown' as any); | ||
| expect(handler).toBeNull(); | ||
| }); | ||
| }); | ||
| describe('pubSub (simplified)', () => { | ||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('calls getHandler for each consumer', async () => { | ||
| const getHandlerMock = jest | ||
| .spyOn(pubsubModule, 'getHandler') | ||
| .mockResolvedValue(() => {}); | ||
|
|
||
| const subscribers = { | ||
| test_event: [1, 2, 3].map(() => ({}) as any), | ||
| }; | ||
|
|
||
| const ps = await pubSub(subscribers); | ||
| const event = new Event(EventName) as any; | ||
| event.detail = { identifier: 'test_event' }; | ||
|
|
||
| ps.dispatchEvent(event); | ||
| await new Promise(process.nextTick); | ||
| expect(getHandlerMock).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| it('does not invoke handler if getHandler returns null', async () => { | ||
| const handlerSpy = jest.fn(); | ||
|
|
||
| jest.spyOn(pubsubModule, 'getHandler').mockResolvedValue(null); // handler will be null | ||
|
|
||
| const subscribers = { | ||
| test_event: [{} as any], | ||
| }; | ||
|
|
||
| const ps = await pubSub(subscribers); | ||
| const event = new Event(EventName) as any; | ||
| event.detail = { identifier: 'test_event' }; | ||
|
|
||
| await new Promise(process.nextTick); | ||
| expect(handlerSpy).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import postgres from 'postgres'; | ||
| import { createSql } from './connection'; | ||
|
|
||
| jest.mock('postgres'); | ||
|
|
||
| describe('createSql', () => { | ||
| it('creates the client only once and returns the same instance', () => { | ||
| const mockSqlInstance = {}; | ||
| (postgres as unknown as jest.Mock).mockReturnValue(mockSqlInstance); | ||
|
|
||
| const getClient = createSql('postgres://test-url', { | ||
| idle_timeout: 1, | ||
| channels: ['test_channel'], | ||
| }); | ||
|
|
||
| const firstCall = getClient(); | ||
| const secondCall = getClient(); | ||
|
|
||
| expect(postgres).toHaveBeenCalledTimes(1); | ||
| expect(firstCall).toBe(secondCall); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { listen } from './listen'; | ||
| import * as connection from './connection'; | ||
|
|
||
| describe('listen', () => { | ||
| const mockListen = jest.fn(); | ||
| const mockSql = { listen: mockListen }; | ||
| const mockDispatchEvent = jest.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| jest.spyOn(connection, 'createSql').mockReturnValue(() => mockSql as any); | ||
|
|
||
| global.CustomEvent = class MockCustomEvent<T> { | ||
| type: string; | ||
| detail: T; | ||
| constructor(type: string, props: { detail: T }) { | ||
| this.type = type; | ||
| this.detail = props.detail; | ||
| } | ||
| } as any; | ||
| }); | ||
|
|
||
| it('throws error if no channels are provided', async () => { | ||
| await expect( | ||
| listen('postgres://test-url', new EventTarget(), {}), | ||
| ).rejects.toThrow('No channels provided'); | ||
| }); | ||
|
|
||
| it('calls sql.listen and dispatches a CustomEvent', async () => { | ||
| const eventTarget = { | ||
| dispatchEvent: mockDispatchEvent, | ||
| } as unknown as EventTarget; | ||
|
|
||
| await listen('postgres://fake-url', eventTarget, { | ||
| channels: ['test_channel'], | ||
| }); | ||
|
|
||
| expect(mockListen).toHaveBeenCalledWith( | ||
| 'test_channel', | ||
| expect.any(Function), | ||
| ); | ||
|
|
||
| const listenCallback = mockListen.mock.calls[0][1]; | ||
| listenCallback('some-value'); | ||
|
|
||
| expect(mockDispatchEvent).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| type: 'notification', | ||
| detail: { | ||
| identifier: 'test_channel', | ||
| data: 'some-value', | ||
| }, | ||
| }), | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { subscirbe } from './subscribe'; | ||
| import * as connection from './connection'; | ||
| import { EventName } from '../core/types'; | ||
|
|
||
| describe('subscirbe', () => { | ||
| const mockSubscribe = jest.fn(); | ||
| const mockSql = { subscribe: mockSubscribe }; | ||
| const mockDispatchEvent = jest.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| jest.spyOn(connection, 'createSql').mockReturnValue(() => mockSql as any); | ||
|
|
||
| global.CustomEvent = class MockCustomEvent<T> { | ||
| type: string; | ||
| detail: T; | ||
| constructor(type: string, props: { detail: T }) { | ||
| this.type = type; | ||
| this.detail = props.detail; | ||
| } | ||
| } as any; | ||
| }); | ||
|
|
||
| it('calls sql.subscribe with "*:inbound"', async () => { | ||
| await subscirbe('postgres://url', new EventTarget(), {}); | ||
|
|
||
| expect(mockSubscribe).toHaveBeenCalledWith( | ||
| '*:inbound', | ||
| expect.any(Function), | ||
| ); | ||
| }); | ||
|
|
||
| it('dispatches event on insert', async () => { | ||
| const eventTarget = { | ||
| dispatchEvent: mockDispatchEvent, | ||
| } as unknown as EventTarget; | ||
|
|
||
| await subscirbe('postgres://url', eventTarget, {}); | ||
|
|
||
| const callback = mockSubscribe.mock.calls[0][1]; | ||
|
|
||
| const row = { id: 1 }; | ||
| const data = { | ||
| command: 'insert', | ||
| relation: { table: 'inbound' }, | ||
| }; | ||
|
|
||
| callback(row, data); | ||
|
|
||
| expect(mockDispatchEvent).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| type: EventName, | ||
| detail: { | ||
| identifier: 'inbound', | ||
| data: { | ||
| tableName: 'inbound', | ||
| action: 'insert', | ||
| new: row, | ||
| old: null, | ||
| }, | ||
| }, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it('does not dispatch event if match returns null', async () => { | ||
| const eventTarget = { | ||
| dispatchEvent: mockDispatchEvent, | ||
| } as unknown as EventTarget; | ||
|
|
||
| await subscirbe('postgres://url', eventTarget, {}); | ||
|
|
||
| const callback = mockSubscribe.mock.calls[0][1]; | ||
|
|
||
| callback({}, { command: 'unknown' }); | ||
|
|
||
| expect(mockDispatchEvent).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did we export this ?