addons/isl/src/__tests__/UnsavedFiles.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, waitFor} from '@testing-library/react';
b69ab319import {nextTick} from 'shared/testUtils';
b69ab3110import App from '../App';
b69ab3111import {
b69ab3112 closeCommitInfoSidebar,
b69ab3113 COMMIT,
b69ab3114 expectMessageNOTSentToServer,
b69ab3115 expectMessageSentToServer,
b69ab3116 resetTestMessages,
b69ab3117 simulateCommits,
b69ab3118 simulateMessageFromServer,
b69ab3119 simulateRepoConnected,
b69ab3120 simulateUncommittedChangedFiles,
b69ab3121} from '../testUtils';
b69ab3122
b69ab3123describe('UnsavedFiles', () => {
b69ab3124 beforeEach(() => {
b69ab3125 resetTestMessages();
b69ab3126 render(<App />);
b69ab3127 act(() => {
b69ab3128 simulateRepoConnected();
b69ab3129 closeCommitInfoSidebar();
b69ab3130 expectMessageSentToServer({
b69ab3131 type: 'subscribe',
b69ab3132 kind: 'smartlogCommits',
b69ab3133 subscriptionID: expect.anything(),
b69ab3134 });
b69ab3135 simulateCommits({
b69ab3136 value: [
b69ab3137 COMMIT('1', 'some public base', '0', {phase: 'public'}),
b69ab3138 COMMIT('a', 'My Commit', '1'),
b69ab3139 COMMIT('b', 'Another Commit', 'a', {isDot: true}),
b69ab3140 ],
b69ab3141 });
b69ab3142 simulateUncommittedChangedFiles({value: [{path: 'foo.txt', status: 'M'}]});
b69ab3143 });
b69ab3144 });
b69ab3145
b69ab3146 it('subscribes to changes to unsaved files', () => {
b69ab3147 expectMessageSentToServer({
b69ab3148 type: 'platform/subscribeToUnsavedFiles',
b69ab3149 });
b69ab3150 });
b69ab3151
b69ab3152 it('shows badge with unsaved files', () => {
b69ab3153 act(() =>
b69ab3154 simulateMessageFromServer({
b69ab3155 type: 'platform/unsavedFiles',
b69ab3156 unsaved: [{path: 'foo.txt', uri: 'file:///foo.txt'}],
b69ab3157 }),
b69ab3158 );
b69ab3159
b69ab3160 expect(screen.getByText('1 unsaved file')).toBeInTheDocument();
b69ab3161
b69ab3162 act(() =>
b69ab3163 simulateMessageFromServer({
b69ab3164 type: 'platform/unsavedFiles',
b69ab3165 unsaved: [
b69ab3166 {path: 'foo.txt', uri: 'file:///foo.txt'},
b69ab3167 {path: 'bar.txt', uri: 'file:///bar.txt'},
b69ab3168 ],
b69ab3169 }),
b69ab3170 );
b69ab3171
b69ab3172 expect(screen.getByText('2 unsaved files')).toBeInTheDocument();
b69ab3173 });
b69ab3174
b69ab3175 describe('confirms before commit/amend', () => {
b69ab3176 beforeEach(() => {
b69ab3177 act(() =>
b69ab3178 simulateMessageFromServer({
b69ab3179 type: 'platform/unsavedFiles',
b69ab3180 unsaved: [{path: 'foo.txt', uri: 'file:///foo.txt'}],
b69ab3181 }),
b69ab3182 );
b69ab3183 });
b69ab3184
b69ab3185 it('allows saving all files', async () => {
b69ab3186 fireEvent.click(screen.getByText('Commit'));
b69ab3187 expect(screen.getByText('You have 1 unsaved file')).toBeInTheDocument();
b69ab3188 fireEvent.click(screen.getByText('Save All and Continue'));
b69ab3189 await waitFor(() => {
b69ab3190 expectMessageSentToServer({
b69ab3191 type: 'platform/saveAllUnsavedFiles',
b69ab3192 });
b69ab3193 });
b69ab3194 act(() => {
b69ab3195 simulateMessageFromServer({
b69ab3196 type: 'platform/savedAllUnsavedFiles',
b69ab3197 success: true,
b69ab3198 });
b69ab3199 });
b69ab31100 await waitFor(() =>
b69ab31101 expectMessageSentToServer({
b69ab31102 type: 'runOperation',
b69ab31103 operation: expect.objectContaining({
b69ab31104 args: expect.arrayContaining(['commit']),
b69ab31105 }),
b69ab31106 }),
b69ab31107 );
b69ab31108 });
b69ab31109
b69ab31110 it('allows continuing without saving', async () => {
b69ab31111 fireEvent.click(screen.getByText('Commit'));
b69ab31112 expect(screen.getByText('You have 1 unsaved file')).toBeInTheDocument();
b69ab31113 fireEvent.click(screen.getByText('Continue Without Saving'));
b69ab31114 expectMessageNOTSentToServer({
b69ab31115 type: 'platform/saveAllUnsavedFiles',
b69ab31116 });
b69ab31117 await waitFor(() =>
b69ab31118 expectMessageSentToServer({
b69ab31119 type: 'runOperation',
b69ab31120 operation: expect.objectContaining({
b69ab31121 args: expect.arrayContaining(['commit']),
b69ab31122 }),
b69ab31123 }),
b69ab31124 );
b69ab31125 });
b69ab31126
b69ab31127 it('allows cancelling commit', async () => {
b69ab31128 fireEvent.click(screen.getByText('Commit'));
b69ab31129 expect(screen.getByText('You have 1 unsaved file')).toBeInTheDocument();
b69ab31130 fireEvent.click(screen.getByText('Cancel'));
b69ab31131 await nextTick();
b69ab31132 expectMessageNOTSentToServer({
b69ab31133 type: 'platform/saveAllUnsavedFiles',
b69ab31134 });
b69ab31135 expectMessageNOTSentToServer({
b69ab31136 type: 'runOperation',
b69ab31137 operation: expect.anything(),
b69ab31138 });
b69ab31139 });
b69ab31140 });
b69ab31141});