/
UseLoginStateManager.test.tsx
86 lines (69 loc) · 2.5 KB
/
UseLoginStateManager.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { default as liffMock } from '@line/liff';
import { FC } from 'react';
import { useLoginStateManager } from '#/use-login-state-manager';
import { fireEvent, render, waitFor } from '@testing-library/react';
jest.mock('@line/liff');
const TestComponent: FC<{ liff?: any }> = ({ liff }) => {
const [isLoggedIn, customLiff] = useLoginStateManager(liff);
return (
<>
<p data-testid="logged-in">{isLoggedIn.toString()}</p>
<button data-testid="login" onClick={customLiff.login}>
login
</button>
<button data-testid="logout" onClick={customLiff.logout}>
logout
</button>
</>
);
};
describe('useLoginStateManager', () => {
const subject = (liff?: any) => render(<TestComponent liff={liff} />);
describe('When liff is undefined', () => {
it('returns false as initial login status', async () => {
const { getByTestId } = subject();
await waitFor(() => {
expect(getByTestId('logged-in').textContent).toBe('false');
});
});
it('returns false as login status after login', async () => {
const { getByTestId } = subject();
fireEvent.click(getByTestId('login'));
await waitFor(() => {
expect(getByTestId('logged-in').textContent).toBe('false');
});
});
it('returns false as login status after logout', async () => {
const { getByTestId } = subject();
fireEvent.click(getByTestId('logout'));
await waitFor(() => {
expect(getByTestId('logged-in').textContent).toBe('false');
});
});
});
describe('When liff is defined', () => {
it('returns false as initial login status', async () => {
const { getByTestId } = subject(liffMock);
await waitFor(() => {
expect(getByTestId('logged-in').textContent).toBe('false');
expect(liffMock.isLoggedIn).toHaveBeenCalled();
});
});
it('returns true as login status after login', async () => {
const { getByTestId } = subject(liffMock);
fireEvent.click(getByTestId('login'));
await waitFor(() => {
expect(getByTestId('logged-in').textContent).toBe('true');
expect(liffMock.login).toHaveBeenCalled();
});
});
it('returns false as login status after logout', async () => {
const { getByTestId } = subject(liffMock);
fireEvent.click(getByTestId('logout'));
await waitFor(() => {
expect(getByTestId('logged-in').textContent).toBe('false');
expect(liffMock.logout).toHaveBeenCalled();
});
});
});
});