|
1 | | -import { renderHook } from '@testing-library/react'; |
2 | | -import { describe, expect, it, vi } from 'vitest'; |
3 | | - |
| 1 | +import { act, renderHook } from '@testing-library/react'; |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
4 | 3 | import StompProvider, { useStompClient } from './StompProvider'; |
5 | 4 |
|
6 | | -vi.mock('@stomp/stompjs', () => { |
| 5 | +const brokerURL = 'ws://localhost:8080'; |
| 6 | + |
| 7 | +const { MockClient } = vi.hoisted(() => { |
7 | 8 | return { |
8 | | - Client: vi.fn().mockImplementation(() => { |
9 | | - return { |
10 | | - activate: vi.fn(), |
11 | | - deactivate: vi.fn(), |
12 | | - onConnect: null, |
13 | | - onDisconnect: null, |
14 | | - onWebSocketError: null, |
15 | | - onStompError: null, |
16 | | - }; |
| 9 | + MockClient: vi.fn(function (this: any, config: any) { |
| 10 | + this.brokerURL = config.brokerURL; |
| 11 | + this.activate = vi.fn(); |
| 12 | + this.deactivate = vi.fn(); |
| 13 | + this.onConnect = null; |
| 14 | + this.onDisconnect = null; |
| 15 | + this.onWebSocketError = null; |
| 16 | + this.onStompError = null; |
17 | 17 | }), |
18 | 18 | }; |
19 | 19 | }); |
20 | 20 |
|
| 21 | +vi.mock('@stomp/stompjs', () => { |
| 22 | + return { |
| 23 | + Client: MockClient, |
| 24 | + }; |
| 25 | +}); |
| 26 | + |
21 | 27 | describe('useStompClient 테스트', () => { |
| 28 | + beforeEach(() => { |
| 29 | + vi.clearAllMocks(); |
| 30 | + }); |
| 31 | + |
22 | 32 | it('useStompClient hook은 StompProvider 외부에서 사용하면 에러를 던진다.', () => { |
23 | 33 | expect(() => renderHook(() => useStompClient())).toThrowError(); |
24 | 34 | }); |
25 | 35 |
|
26 | | - it('useStompClient hook은 StompProvider 내부에서 사용하면 정상 작동한다.', () => { |
| 36 | + it('초기 상태에서는 connected가 false이다.', () => { |
27 | 37 | const { result } = renderHook(() => useStompClient(), { |
28 | 38 | wrapper: ({ children }) => ( |
29 | | - <StompProvider brokerURL="">{children}</StompProvider> |
| 39 | + <StompProvider brokerURL={brokerURL}>{children}</StompProvider> |
30 | 40 | ), |
31 | 41 | }); |
32 | 42 |
|
33 | 43 | expect(result.current).toHaveProperty('client'); |
| 44 | + expect(result.current.client).toBeTruthy(); |
34 | 45 | expect(result.current).toHaveProperty('connected'); |
| 46 | + expect(result.current.connected).toBe(false); |
| 47 | + }); |
| 48 | + |
| 49 | + it('onConnect 콜백이 호출되면 connected가 true가 된다.', () => { |
| 50 | + const { result } = renderHook(() => useStompClient(), { |
| 51 | + wrapper: ({ children }) => ( |
| 52 | + <StompProvider brokerURL={brokerURL}>{children}</StompProvider> |
| 53 | + ), |
| 54 | + }); |
| 55 | + |
| 56 | + expect(result.current.connected).toBe(false); |
| 57 | + |
| 58 | + act(() => { |
| 59 | + const clientInstance = MockClient.mock.instances[0] as any; |
| 60 | + clientInstance?.onConnect?.(); |
| 61 | + }); |
| 62 | + |
| 63 | + expect(result.current.connected).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it('onDisconnect 콜백이 호출되면 connected가 false가 된다.', () => { |
| 67 | + const { result } = renderHook(() => useStompClient(), { |
| 68 | + wrapper: ({ children }) => ( |
| 69 | + <StompProvider brokerURL={brokerURL}>{children}</StompProvider> |
| 70 | + ), |
| 71 | + }); |
| 72 | + |
| 73 | + const clientInstance = MockClient.mock.instances[0] as any; |
| 74 | + act(() => { |
| 75 | + clientInstance?.onConnect?.(); |
| 76 | + }); |
| 77 | + expect(result.current.connected).toBe(true); |
| 78 | + |
| 79 | + act(() => { |
| 80 | + clientInstance?.onDisconnect?.(); |
| 81 | + }); |
| 82 | + |
| 83 | + expect(result.current.connected).toBe(false); |
| 84 | + }); |
| 85 | + |
| 86 | + it('onWebSocketError 콜백이 호출되면 connected가 false가 된다.', () => { |
| 87 | + const { result } = renderHook(() => useStompClient(), { |
| 88 | + wrapper: ({ children }) => ( |
| 89 | + <StompProvider brokerURL={brokerURL}>{children}</StompProvider> |
| 90 | + ), |
| 91 | + }); |
| 92 | + |
| 93 | + const clientInstance = MockClient.mock.instances[0] as any; |
| 94 | + act(() => { |
| 95 | + clientInstance?.onConnect?.(); |
| 96 | + }); |
| 97 | + expect(result.current.connected).toBe(true); |
| 98 | + |
| 99 | + act(() => { |
| 100 | + const mockError = new Error('WebSocket connection failed'); |
| 101 | + clientInstance?.onWebSocketError?.(mockError); |
| 102 | + }); |
| 103 | + |
| 104 | + expect(result.current.connected).toBe(false); |
35 | 105 | }); |
36 | 106 | }); |
0 commit comments