|
| 1 | +import React from 'react'; |
| 2 | +import '@testing-library/jest-dom/extend-expect'; |
| 3 | +import {act} from '@testing-library/react'; |
| 4 | +import {renderHook} from '@testing-library/react-hooks'; |
| 5 | + |
| 6 | +import { |
| 7 | + ReviewStateProvider, |
| 8 | + useCommentThreads |
| 9 | +} from 'review/ReviewStateProvider'; |
| 10 | +import { |
| 11 | + postReviewStateResetMessage, |
| 12 | + postReviewStateThreadChangeMessage |
| 13 | +} from 'review/postMessage'; |
| 14 | + |
| 15 | +function wrapper({children}) { |
| 16 | + return <ReviewStateProvider>{children}</ReviewStateProvider>; |
| 17 | +} |
| 18 | + |
| 19 | +function postReset(payload) { |
| 20 | + act(() => { |
| 21 | + postReviewStateResetMessage(window, payload); |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +function postThreadChange(payload) { |
| 26 | + act(() => { |
| 27 | + postReviewStateThreadChangeMessage(window, payload); |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +describe('ReviewStateProvider', () => { |
| 32 | + it('provides empty initial state', () => { |
| 33 | + const {result} = renderHook( |
| 34 | + () => useCommentThreads('CE', 10), |
| 35 | + {wrapper} |
| 36 | + ); |
| 37 | + |
| 38 | + expect(result.current).toEqual([]); |
| 39 | + }); |
| 40 | + |
| 41 | + it('updates state on reset message', async () => { |
| 42 | + const {result, waitForNextUpdate} = renderHook( |
| 43 | + () => useCommentThreads('CE', 10), |
| 44 | + {wrapper} |
| 45 | + ); |
| 46 | + |
| 47 | + postReset({ |
| 48 | + currentUser: {id: 42, name: 'Alice'}, |
| 49 | + commentThreads: [ |
| 50 | + {id: 1, subjectType: 'CE', subjectId: 10, comments: []}, |
| 51 | + {id: 2, subjectType: 'CE', subjectId: 20, comments: []} |
| 52 | + ] |
| 53 | + }); |
| 54 | + |
| 55 | + await waitForNextUpdate(); |
| 56 | + |
| 57 | + expect(result.current).toHaveLength(1); |
| 58 | + expect(result.current[0].id).toBe(1); |
| 59 | + }); |
| 60 | + |
| 61 | + it('updates single thread on thread change message', async () => { |
| 62 | + const {result, waitForNextUpdate} = renderHook( |
| 63 | + () => useCommentThreads('CE', 10), |
| 64 | + {wrapper} |
| 65 | + ); |
| 66 | + |
| 67 | + postReset({ |
| 68 | + currentUser: {id: 42}, |
| 69 | + commentThreads: [ |
| 70 | + {id: 1, subjectType: 'CE', subjectId: 10, comments: []} |
| 71 | + ] |
| 72 | + }); |
| 73 | + |
| 74 | + await waitForNextUpdate(); |
| 75 | + expect(result.current[0].comments).toHaveLength(0); |
| 76 | + |
| 77 | + postThreadChange({ |
| 78 | + id: 1, |
| 79 | + subjectType: 'CE', |
| 80 | + subjectId: 10, |
| 81 | + comments: [{id: 100, body: 'Hello'}] |
| 82 | + }); |
| 83 | + |
| 84 | + await waitForNextUpdate(); |
| 85 | + expect(result.current[0].comments).toHaveLength(1); |
| 86 | + }); |
| 87 | + |
| 88 | + it('ignores messages from different origins', () => { |
| 89 | + const {result} = renderHook( |
| 90 | + () => useCommentThreads('CE', 10), |
| 91 | + {wrapper} |
| 92 | + ); |
| 93 | + |
| 94 | + act(() => { |
| 95 | + window.dispatchEvent(new MessageEvent('message', { |
| 96 | + data: { |
| 97 | + type: 'REVIEW_STATE_RESET', |
| 98 | + payload: { |
| 99 | + currentUser: {id: 99}, |
| 100 | + commentThreads: [{id: 1, subjectType: 'CE', subjectId: 10, comments: []}] |
| 101 | + } |
| 102 | + }, |
| 103 | + origin: 'https://evil.example.com' |
| 104 | + })); |
| 105 | + }); |
| 106 | + |
| 107 | + expect(result.current).toEqual([]); |
| 108 | + }); |
| 109 | +}); |
0 commit comments