Skip to content

Commit a5d1a94

Browse files
committed
Disable local and session storage at the end of each test
1 parent 78913d5 commit a5d1a94

File tree

6 files changed

+44
-5
lines changed

6 files changed

+44
-5
lines changed

tests/spec/spec_helper.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,15 @@
7979
"[data-test-id = '#{id_s}']"
8080
end
8181
end
82+
83+
RSpec.configure do |config|
84+
config.after(:example, :js) do
85+
page.execute_script <<~JS
86+
(() => {
87+
if (window.rustPlayground) {
88+
window.rustPlayground.disableSyncChangesToStorage();
89+
}
90+
})()
91+
JS
92+
end
93+
end

ui/frontend/actions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const createAction = <T extends string, P extends {}>(type: T, props?: P) => (
6262

6363
export enum ActionType {
6464
InitializeApplication = 'INITIALIZE_APPLICATION',
65+
DisableSyncChangesToStorage = 'DISABLE_SYNC_CHANGES_TO_STORAGE',
6566
SetPage = 'SET_PAGE',
6667
ChangeEditor = 'CHANGE_EDITOR',
6768
ChangeKeybinding = 'CHANGE_KEYBINDING',
@@ -131,6 +132,8 @@ export enum ActionType {
131132

132133
export const initializeApplication = () => createAction(ActionType.InitializeApplication);
133134

135+
export const disableSyncChangesToStorage = () => createAction(ActionType.DisableSyncChangesToStorage);
136+
134137
const setPage = (page: Page) =>
135138
createAction(ActionType.SetPage, { page });
136139

@@ -897,6 +900,7 @@ export function showExample(code: string): ThunkAction {
897900

898901
export type Action =
899902
| ReturnType<typeof initializeApplication>
903+
| ReturnType<typeof disableSyncChangesToStorage>
900904
| ReturnType<typeof setPage>
901905
| ReturnType<typeof changePairCharacters>
902906
| ReturnType<typeof changeAssemblyFlavor>

ui/frontend/configureStore.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,18 @@ export default function configureStore(window: Window) {
3636

3737
store.subscribe(() => {
3838
const state = store.getState();
39-
localStorage.saveChanges(state);
40-
sessionStorage.saveChanges(state);
39+
40+
// Some automated tests run fast enough that the following interleaving is possible:
41+
//
42+
// 1. RSpec test finishes, local/session storage cleared
43+
// 2. WebSocket connects, the state updates, and the local/session storage is saved
44+
// 3. Subsequent RSpec test starts and local/session storage has been preserved
45+
//
46+
// We allow the tests to stop saving to sidestep that.
47+
if (state.globalConfiguration.syncChangesToStorage) {
48+
localStorage.saveChanges(state);
49+
sessionStorage.saveChanges(state);
50+
}
4151
})
4252

4353
return store;

ui/frontend/declarations.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ interface Window {
2020
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__: any;
2121
rustPlayground: {
2222
setCode(code: string): void;
23+
disableSyncChangesToStorage(): void;
2324
webSocket: WebSocket | null;
2425
};
2526
}

ui/frontend/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Provider } from 'react-redux';
1010

1111
import {
1212
editCode,
13+
disableSyncChangesToStorage,
1314
enableFeatureGate,
1415
gotoPosition,
1516
selectText,
@@ -53,6 +54,9 @@ window.rustPlayground = {
5354
setCode: code => {
5455
store.dispatch(editCode(code));
5556
},
57+
disableSyncChangesToStorage: () => {
58+
store.dispatch(disableSyncChangesToStorage());
59+
},
5660
// Temporarily storing this as a global to prevent it from being
5761
// garbage collected (at least by Safari).
5862
webSocket: socket,
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import { Action } from '../actions';
1+
import { Action, ActionType } from '../actions';
22

33
export interface State {
44
baseUrl: string;
5+
syncChangesToStorage: boolean;
56
}
67

78
const DEFAULT: State = {
89
baseUrl: '',
10+
syncChangesToStorage: true,
911
};
1012

11-
export default function globalConfiguration(state = DEFAULT, _action: Action): State {
12-
return state;
13+
export default function globalConfiguration(state = DEFAULT, action: Action): State {
14+
switch (action.type) {
15+
case ActionType.DisableSyncChangesToStorage: {
16+
return { ...state, syncChangesToStorage: false };
17+
}
18+
default:
19+
return state;
20+
}
1321
}

0 commit comments

Comments
 (0)