| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import {act, render, screen} from '@testing-library/react'; |
| 9 | import App from '../App'; |
| 10 | import platform from '../platform'; |
| 11 | import {CommitInfoTestUtils} from '../testQueries'; |
| 12 | import { |
| 13 | COMMIT, |
| 14 | expectMessageSentToServer, |
| 15 | simulateCommits, |
| 16 | simulateRepoConnected, |
| 17 | } from '../testUtils'; |
| 18 | |
| 19 | describe('persistAtomToLocalStorageEffect', () => { |
| 20 | const getTemporary = jest.fn(); |
| 21 | const setTemporary = jest.fn(); |
| 22 | |
| 23 | beforeEach(() => { |
| 24 | platform.getPersistedState = getTemporary; |
| 25 | platform.setPersistedState = setTemporary; |
| 26 | getTemporary.mockReset(); |
| 27 | setTemporary.mockReset(); |
| 28 | |
| 29 | getTemporary.mockImplementation(() => null); |
| 30 | setTemporary.mockImplementation(() => null); |
| 31 | |
| 32 | render(<App />); |
| 33 | |
| 34 | act(() => { |
| 35 | simulateRepoConnected(); |
| 36 | expectMessageSentToServer({ |
| 37 | type: 'subscribe', |
| 38 | kind: 'smartlogCommits', |
| 39 | subscriptionID: expect.anything(), |
| 40 | }); |
| 41 | simulateCommits({ |
| 42 | value: [ |
| 43 | COMMIT('1', 'some public base', '0', {phase: 'public'}), |
| 44 | COMMIT('a', 'My Commit', '1'), |
| 45 | COMMIT('b', 'Another Commit', 'a', {isDot: true}), |
| 46 | ], |
| 47 | }); |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | it('saves state to local storage', () => { |
| 52 | expect(screen.getByTestId('commit-info-view')).toBeInTheDocument(); |
| 53 | |
| 54 | act(() => { |
| 55 | CommitInfoTestUtils.openCommitInfoSidebar(); // toggle |
| 56 | }); |
| 57 | |
| 58 | expect(screen.queryByTestId('commit-info-view')).not.toBeInTheDocument(); |
| 59 | |
| 60 | expect(platform.setPersistedState).toHaveBeenCalledWith( |
| 61 | 'isl.drawer-state', |
| 62 | expect.objectContaining({ |
| 63 | right: {collapsed: true, size: 500}, |
| 64 | }), |
| 65 | ); |
| 66 | |
| 67 | act(() => { |
| 68 | CommitInfoTestUtils.openCommitInfoSidebar(); // toggle |
| 69 | }); |
| 70 | |
| 71 | expect(platform.setPersistedState).toHaveBeenCalledWith( |
| 72 | 'isl.drawer-state', |
| 73 | expect.objectContaining({ |
| 74 | right: {collapsed: false, size: 500}, |
| 75 | }), |
| 76 | ); |
| 77 | }); |
| 78 | |
| 79 | it.skip('loads state on startup', () => { |
| 80 | // mock seems to happen too late to capture the getPersistedState call. |
| 81 | // but I verified that getPersistedState is called using console log. |
| 82 | expect(platform.getPersistedState).toHaveBeenCalledWith('isl.drawer-state'); |
| 83 | }); |
| 84 | }); |
| 85 | |