addons/isl/src/__tests__/alerts.test.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {act, fireEvent, render, screen} from '@testing-library/react';
b69ab319import App from '../App';
b69ab3110import {
b69ab3111 closeCommitInfoSidebar,
b69ab3112 COMMIT,
b69ab3113 expectMessageSentToServer,
b69ab3114 simulateCommits,
b69ab3115 simulateMessageFromServer,
b69ab3116 simulateRepoConnected,
b69ab3117} from '../testUtils';
b69ab3118
b69ab3119describe('cwd', () => {
b69ab3120 beforeEach(() => {
b69ab3121 render(<App />);
b69ab3122 act(() => {
b69ab3123 simulateRepoConnected();
b69ab3124 closeCommitInfoSidebar();
b69ab3125 expectMessageSentToServer({
b69ab3126 type: 'subscribe',
b69ab3127 kind: 'smartlogCommits',
b69ab3128 subscriptionID: expect.anything(),
b69ab3129 });
b69ab3130 simulateCommits({
b69ab3131 value: [
b69ab3132 COMMIT('1', 'some public base', '0', {phase: 'public'}),
b69ab3133 COMMIT('a', 'My Commit', '1'),
b69ab3134 COMMIT('b', 'Another Commit', 'a', {isDot: true}),
b69ab3135 ],
b69ab3136 });
b69ab3137 });
b69ab3138 });
b69ab3139
b69ab3140 it('fetches alerts', () => {
b69ab3141 expectMessageSentToServer({type: 'fetchActiveAlerts'});
b69ab3142 });
b69ab3143
b69ab3144 it('shows alerts', () => {
b69ab3145 expectMessageSentToServer({type: 'fetchActiveAlerts'});
b69ab3146 act(() => {
b69ab3147 simulateMessageFromServer({
b69ab3148 type: 'fetchedActiveAlerts',
b69ab3149 alerts: [
b69ab3150 {
b69ab3151 key: 'test',
b69ab3152 title: 'Test Alert',
b69ab3153 description: 'This is a test',
b69ab3154 severity: 'SEV 4',
b69ab3155 url: 'https://sapling-scm.com',
b69ab3156 ['show-in-isl']: true,
b69ab3157 },
b69ab3158 ],
b69ab3159 });
b69ab3160 });
b69ab3161 expect(screen.getByText('Test Alert')).toBeInTheDocument();
b69ab3162 expect(screen.getByText('This is a test')).toBeInTheDocument();
b69ab3163 expect(screen.getByText('SEV 4')).toBeInTheDocument();
b69ab3164 });
b69ab3165
b69ab3166 describe('version matching', () => {
b69ab3167 const simulateApplicationInfo = (version: string) => {
b69ab3168 act(() => {
b69ab3169 simulateMessageFromServer({
b69ab3170 type: 'applicationInfo',
b69ab3171 info: {
b69ab3172 version,
b69ab3173 logFilePath: '',
b69ab3174 platformName: 'vscode',
b69ab3175 },
b69ab3176 });
b69ab3177 });
b69ab3178 };
b69ab3179
b69ab3180 const simulateAlert = (regex: string | undefined) => {
b69ab3181 act(() => {
b69ab3182 simulateMessageFromServer({
b69ab3183 type: 'fetchedActiveAlerts',
b69ab3184 alerts: [
b69ab3185 {
b69ab3186 key: 'version_test',
b69ab3187 title: 'Test Alert',
b69ab3188 description: 'This is a test',
b69ab3189 severity: 'SEV 4',
b69ab3190 url: 'https://sapling-scm.com',
b69ab3191 ['show-in-isl']: true,
b69ab3192 ['isl-version-regex']: regex,
b69ab3193 },
b69ab3194 ],
b69ab3195 });
b69ab3196 });
b69ab3197 };
b69ab3198
b69ab3199 it('shows alerts matching current version', () => {
b69ab31100 simulateApplicationInfo('0.1.38000');
b69ab31101 simulateAlert('^0.1.38.*$');
b69ab31102 expect(screen.getByText('Test Alert')).toBeInTheDocument();
b69ab31103 });
b69ab31104
b69ab31105 it('hides alerts not matching current version', () => {
b69ab31106 simulateApplicationInfo('0.1.36000');
b69ab31107 simulateAlert('^0.1.38.*$');
b69ab31108 expect(screen.queryByText('Test Alert')).not.toBeInTheDocument();
b69ab31109 });
b69ab31110
b69ab31111 it('shows alerts missing regex', () => {
b69ab31112 simulateApplicationInfo('0.1.36000');
b69ab31113 simulateAlert(undefined);
b69ab31114 expect(screen.getByText('Test Alert')).toBeInTheDocument();
b69ab31115 });
b69ab31116
b69ab31117 it('hides alerts when regex given, while app info is loading', () => {
b69ab31118 simulateAlert('^0.1.38.*$');
b69ab31119 expect(screen.queryByText('Test Alert')).not.toBeInTheDocument();
b69ab31120 simulateApplicationInfo('0.1.38');
b69ab31121 expect(screen.getByText('Test Alert')).toBeInTheDocument();
b69ab31122 });
b69ab31123 });
b69ab31124
b69ab31125 it('dismiss alerts', () => {
b69ab31126 expectMessageSentToServer({type: 'fetchActiveAlerts'});
b69ab31127 act(() => {
b69ab31128 simulateMessageFromServer({
b69ab31129 type: 'fetchedActiveAlerts',
b69ab31130 alerts: [
b69ab31131 {
b69ab31132 key: 'test-dismiss',
b69ab31133 title: 'Test Alert',
b69ab31134 description: 'This is a test',
b69ab31135 severity: 'SEV 4',
b69ab31136 url: 'https://sapling-scm.com',
b69ab31137 ['show-in-isl']: true,
b69ab31138 },
b69ab31139 ],
b69ab31140 });
b69ab31141 });
b69ab31142
b69ab31143 expect(screen.getByText('Test Alert')).toBeInTheDocument();
b69ab31144 act(() => {
b69ab31145 fireEvent.click(screen.getByTestId('dismiss-alert'));
b69ab31146 });
b69ab31147 const found = localStorage.getItem('isl.dismissed-alerts');
b69ab31148 expect(found).toEqual(JSON.stringify({'test-dismiss': true}));
b69ab31149
b69ab31150 expect(screen.queryByText('Test Alert')).not.toBeInTheDocument();
b69ab31151 });
b69ab31152});