Skip to content

Commit

Permalink
fixup! fix(js): Remove @novu/shared dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
SokratisVidros committed Nov 29, 2024
1 parent eb6a652 commit b1ef2fb
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions packages/js/src/base-module.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { InboxService } from './api';
import { BaseModule } from './base-module';
import { NovuEventEmitter } from './event-emitter';

beforeAll(() => jest.spyOn(global, 'fetch'));
afterAll(() => jest.restoreAllMocks());

describe('callWithSession(fn)', () => {
test('should invoke callback function immediately if session is initialized', async () => {
const emitter = new NovuEventEmitter();
const bm = new BaseModule({
inboxServiceInstance: {
isSessionInitialized: true,
} as InboxService,
eventEmitterInstance: emitter,
});

const cb = jest.fn();
bm.callWithSession(cb);
expect(cb).toHaveBeenCalled();
});

test('should invoke callback function as soon as session is initialized', async () => {
const emitter = new NovuEventEmitter();
const bm = new BaseModule({
inboxServiceInstance: {} as InboxService,
eventEmitterInstance: emitter,
});

const cb = jest.fn();

bm.callWithSession(cb);
expect(cb).not.toHaveBeenCalled();

emitter.emit('session.initialize.resolved', {
args: {
applicationIdentifier: 'foo',
subscriberId: 'bar',
},
data: {
token: 'cafebabe',
totalUnreadCount: 10,
removeNovuBranding: true,
},
});

expect(cb).toHaveBeenCalled();
});

test('should return an error if session initialization failed', async () => {
const emitter = new NovuEventEmitter();
const bm = new BaseModule({
inboxServiceInstance: {} as InboxService,
eventEmitterInstance: emitter,
});

emitter.emit('session.initialize.resolved', {
args: {
applicationIdentifier: 'foo',
subscriberId: 'bar',
},
error: new Error('Failed to initialize session'),
});

const cb = jest.fn();
const result = await bm.callWithSession(cb);
expect(result).toEqual({
error: new Error('Failed to initialize session, please contact the support'),
});
});
});

0 comments on commit b1ef2fb

Please sign in to comment.