Skip to content

Commit e5fb0e2

Browse files
committed
Better tests :)
1 parent 177a276 commit e5fb0e2

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

packages/hub/test/hub.test.ts

+27-17
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ const clientFn: any = jest.fn();
66

77
describe('Hub', () => {
88
afterEach(() => {
9-
jest.resetAllMocks();
9+
jest.restoreAllMocks();
1010
jest.useRealTimers();
1111
});
1212

13+
test('call bindClient with provided client when constructing new instance', () => {
14+
const testClient: any = { setupIntegrations: jest.fn() };
15+
const spy = jest.spyOn(Hub.prototype, 'bindClient');
16+
new Hub(testClient);
17+
expect(spy).toHaveBeenCalledWith(testClient);
18+
});
19+
1320
test('push process into stack', () => {
1421
const hub = new Hub();
1522
expect(hub.getStack()).toHaveLength(1);
@@ -52,30 +59,33 @@ describe('Hub', () => {
5259
});
5360

5461
describe('bindClient', () => {
55-
test('simple', () => {
62+
test('should override curent client', () => {
63+
const testClient: any = { setupIntegrations: jest.fn() };
64+
const nextClient: any = { setupIntegrations: jest.fn() };
65+
const hub = new Hub(testClient);
66+
hub.bindClient(nextClient);
67+
expect(hub.getStack()).toHaveLength(1);
68+
expect(hub.getStack()[0].client).toBe(nextClient);
69+
});
70+
71+
test('should bind client to the top-most layer', () => {
5672
const testClient: any = { bla: 'a' };
73+
const nextClient: any = { foo: 'bar' };
5774
const hub = new Hub(testClient);
58-
const ndClient: any = { foo: 'bar' };
5975
hub.pushScope();
60-
hub.bindClient(ndClient);
76+
hub.bindClient(nextClient);
6177
expect(hub.getStack()).toHaveLength(2);
6278
expect(hub.getStack()[0].client).toBe(testClient);
63-
expect(hub.getStack()[1].client).toBe(ndClient);
64-
});
65-
test('call setupIntegrations', () => {
66-
const setupIntegrations = jest.fn();
67-
const testClient: any = { setupIntegrations };
68-
const hub = new Hub(testClient);
69-
hub.bindClient(testClient);
70-
expect(setupIntegrations).toHaveBeenCalled();
79+
expect(hub.getStack()[1].client).toBe(nextClient);
7180
});
7281

73-
test('call setupIntegrations from constructor', () => {
74-
const setupIntegrations = jest.fn();
75-
const testClient: any = { setupIntegrations };
82+
test('should call setupIntegration method of passed client', () => {
83+
const testClient: any = { setupIntegrations: jest.fn() };
84+
const nextClient: any = { setupIntegrations: jest.fn() };
7685
const hub = new Hub(testClient);
77-
expect(setupIntegrations).toHaveBeenCalled();
78-
expect(hub).toBeTruthy();
86+
hub.bindClient(nextClient);
87+
expect(testClient.setupIntegrations).toHaveBeenCalled();
88+
expect(nextClient.setupIntegrations).toHaveBeenCalled();
7989
});
8090
});
8191

0 commit comments

Comments
 (0)