| 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, fireEvent, render, screen} from '@testing-library/react'; |
| 9 | import App from '../App'; |
| 10 | import { |
| 11 | closeCommitInfoSidebar, |
| 12 | COMMIT, |
| 13 | expectMessageSentToServer, |
| 14 | simulateCommits, |
| 15 | simulateMessageFromServer, |
| 16 | simulateRepoConnected, |
| 17 | } from '../testUtils'; |
| 18 | |
| 19 | describe('cwd', () => { |
| 20 | beforeEach(() => { |
| 21 | render(<App />); |
| 22 | act(() => { |
| 23 | simulateRepoConnected(); |
| 24 | closeCommitInfoSidebar(); |
| 25 | expectMessageSentToServer({ |
| 26 | type: 'subscribe', |
| 27 | kind: 'smartlogCommits', |
| 28 | subscriptionID: expect.anything(), |
| 29 | }); |
| 30 | simulateCommits({ |
| 31 | value: [ |
| 32 | COMMIT('1', 'some public base', '0', {phase: 'public'}), |
| 33 | COMMIT('a', 'My Commit', '1'), |
| 34 | COMMIT('b', 'Another Commit', 'a', {isDot: true}), |
| 35 | ], |
| 36 | }); |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | it('fetches alerts', () => { |
| 41 | expectMessageSentToServer({type: 'fetchActiveAlerts'}); |
| 42 | }); |
| 43 | |
| 44 | it('shows alerts', () => { |
| 45 | expectMessageSentToServer({type: 'fetchActiveAlerts'}); |
| 46 | act(() => { |
| 47 | simulateMessageFromServer({ |
| 48 | type: 'fetchedActiveAlerts', |
| 49 | alerts: [ |
| 50 | { |
| 51 | key: 'test', |
| 52 | title: 'Test Alert', |
| 53 | description: 'This is a test', |
| 54 | severity: 'SEV 4', |
| 55 | url: 'https://sapling-scm.com', |
| 56 | ['show-in-isl']: true, |
| 57 | }, |
| 58 | ], |
| 59 | }); |
| 60 | }); |
| 61 | expect(screen.getByText('Test Alert')).toBeInTheDocument(); |
| 62 | expect(screen.getByText('This is a test')).toBeInTheDocument(); |
| 63 | expect(screen.getByText('SEV 4')).toBeInTheDocument(); |
| 64 | }); |
| 65 | |
| 66 | describe('version matching', () => { |
| 67 | const simulateApplicationInfo = (version: string) => { |
| 68 | act(() => { |
| 69 | simulateMessageFromServer({ |
| 70 | type: 'applicationInfo', |
| 71 | info: { |
| 72 | version, |
| 73 | logFilePath: '', |
| 74 | platformName: 'vscode', |
| 75 | }, |
| 76 | }); |
| 77 | }); |
| 78 | }; |
| 79 | |
| 80 | const simulateAlert = (regex: string | undefined) => { |
| 81 | act(() => { |
| 82 | simulateMessageFromServer({ |
| 83 | type: 'fetchedActiveAlerts', |
| 84 | alerts: [ |
| 85 | { |
| 86 | key: 'version_test', |
| 87 | title: 'Test Alert', |
| 88 | description: 'This is a test', |
| 89 | severity: 'SEV 4', |
| 90 | url: 'https://sapling-scm.com', |
| 91 | ['show-in-isl']: true, |
| 92 | ['isl-version-regex']: regex, |
| 93 | }, |
| 94 | ], |
| 95 | }); |
| 96 | }); |
| 97 | }; |
| 98 | |
| 99 | it('shows alerts matching current version', () => { |
| 100 | simulateApplicationInfo('0.1.38000'); |
| 101 | simulateAlert('^0.1.38.*$'); |
| 102 | expect(screen.getByText('Test Alert')).toBeInTheDocument(); |
| 103 | }); |
| 104 | |
| 105 | it('hides alerts not matching current version', () => { |
| 106 | simulateApplicationInfo('0.1.36000'); |
| 107 | simulateAlert('^0.1.38.*$'); |
| 108 | expect(screen.queryByText('Test Alert')).not.toBeInTheDocument(); |
| 109 | }); |
| 110 | |
| 111 | it('shows alerts missing regex', () => { |
| 112 | simulateApplicationInfo('0.1.36000'); |
| 113 | simulateAlert(undefined); |
| 114 | expect(screen.getByText('Test Alert')).toBeInTheDocument(); |
| 115 | }); |
| 116 | |
| 117 | it('hides alerts when regex given, while app info is loading', () => { |
| 118 | simulateAlert('^0.1.38.*$'); |
| 119 | expect(screen.queryByText('Test Alert')).not.toBeInTheDocument(); |
| 120 | simulateApplicationInfo('0.1.38'); |
| 121 | expect(screen.getByText('Test Alert')).toBeInTheDocument(); |
| 122 | }); |
| 123 | }); |
| 124 | |
| 125 | it('dismiss alerts', () => { |
| 126 | expectMessageSentToServer({type: 'fetchActiveAlerts'}); |
| 127 | act(() => { |
| 128 | simulateMessageFromServer({ |
| 129 | type: 'fetchedActiveAlerts', |
| 130 | alerts: [ |
| 131 | { |
| 132 | key: 'test-dismiss', |
| 133 | title: 'Test Alert', |
| 134 | description: 'This is a test', |
| 135 | severity: 'SEV 4', |
| 136 | url: 'https://sapling-scm.com', |
| 137 | ['show-in-isl']: true, |
| 138 | }, |
| 139 | ], |
| 140 | }); |
| 141 | }); |
| 142 | |
| 143 | expect(screen.getByText('Test Alert')).toBeInTheDocument(); |
| 144 | act(() => { |
| 145 | fireEvent.click(screen.getByTestId('dismiss-alert')); |
| 146 | }); |
| 147 | const found = localStorage.getItem('isl.dismissed-alerts'); |
| 148 | expect(found).toEqual(JSON.stringify({'test-dismiss': true})); |
| 149 | |
| 150 | expect(screen.queryByText('Test Alert')).not.toBeInTheDocument(); |
| 151 | }); |
| 152 | }); |
| 153 | |