-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathuseFlagsStatus.test.tsx
52 lines (42 loc) · 1.68 KB
/
useFlagsStatus.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { renderHook } from '@testing-library/react-hooks/native';
import React from 'react';
import { EVENTS } from 'unleash-proxy-client';
import useFlagsStatus from './useFlagsStatus';
const onMock = jest.fn();
const useContextSpy = jest.spyOn(React, 'useContext');
const givenFlagName: string = 'Test';
const clientMock: any = {
on: jest.fn(),
}
beforeEach(() => {
onMock.mockClear();
})
test('should return flagReady false and flagsErrot null when no event received', () => {
useContextSpy.mockReturnValue({ on: onMock });
const { result } = renderHook(() => useFlagsStatus());
expect(onMock).toHaveBeenCalledWith(EVENTS.READY, expect.any(Function))
expect(onMock).toHaveBeenCalledWith(EVENTS.ERROR, expect.any(Function))
expect(result.current).toStrictEqual({ flagsReady: false, flagsError: null });
expect(onMock).toHaveBeenCalledTimes(2);
});
test('should return flagsReady true when received an READY event', () => {
onMock.mockImplementation((event,cb) => {
if (event === EVENTS.READY) {
cb()
}
});
useContextSpy.mockReturnValue({ on: onMock });
const { result } = renderHook(() => useFlagsStatus());
expect(result.current).toStrictEqual({ flagsReady: true, flagsError: null });
});
test('should return flagError string when received an error event', () => {
const givenError = new Error('Error');
onMock.mockImplementation((event,cb) => {
if (event === EVENTS.ERROR) {
cb(givenError)
}
});
useContextSpy.mockReturnValue({ on: onMock });
const { result } = renderHook(() => useFlagsStatus());
expect(result.current).toStrictEqual({ flagsReady: false, flagsError: givenError });
});