mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 09:36:27 +00:00
fileStorage/reducers: drop use of Set
Using set causes problems with tooling that mocks the global app state, so stick with arrays.
This commit is contained in:
@@ -140,7 +140,7 @@ describe('setEditSession', () => {
|
||||
it('should wait for storage to be initialized', async () => {
|
||||
const mockEditor = mock<monaco.editor.ICodeEditor>();
|
||||
const saga = new AsyncSaga(editor, {
|
||||
fileStorage: { isInitialized: false, fileNames: new Set() },
|
||||
fileStorage: { isInitialized: false, fileNames: [] },
|
||||
});
|
||||
|
||||
saga.put(setEditSession(mockEditor));
|
||||
@@ -155,7 +155,7 @@ describe('setEditSession', () => {
|
||||
it('should load main.py', async () => {
|
||||
const mockEditor = mock<monaco.editor.ICodeEditor>();
|
||||
const saga = new AsyncSaga(editor, {
|
||||
fileStorage: { isInitialized: true, fileNames: new Set(['main.py']) },
|
||||
fileStorage: { isInitialized: true, fileNames: ['main.py'] },
|
||||
});
|
||||
|
||||
saga.put(setEditSession(mockEditor));
|
||||
@@ -175,7 +175,7 @@ describe('setEditSession', () => {
|
||||
it('should not raise error if main.py does not exist', async () => {
|
||||
const mockEditor = mock<monaco.editor.ICodeEditor>();
|
||||
const saga = new AsyncSaga(editor, {
|
||||
fileStorage: { isInitialized: true, fileNames: new Set() },
|
||||
fileStorage: { isInitialized: true, fileNames: [] },
|
||||
});
|
||||
|
||||
saga.put(setEditSession(mockEditor));
|
||||
|
||||
+1
-1
@@ -115,7 +115,7 @@ function* handleSetEditSession(action: ReturnType<typeof setEditSession>): Gener
|
||||
|
||||
const fileList = yield* select((s: RootState) => s.fileStorage.fileNames);
|
||||
|
||||
if (!fileList.has(currentFileName)) {
|
||||
if (!fileList.includes(currentFileName)) {
|
||||
// The file doesn't exist in storage, so don't try to open it.
|
||||
yield* put(didSetEditSession(action.editSession));
|
||||
return;
|
||||
|
||||
@@ -14,7 +14,7 @@ type State = ReturnType<typeof reducers>;
|
||||
test('initial state', () => {
|
||||
expect(reducers(undefined, {} as AnyAction)).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"fileNames": Set {},
|
||||
"fileNames": Array [],
|
||||
"isInitialized": false,
|
||||
}
|
||||
`);
|
||||
@@ -33,31 +33,31 @@ test('fileNames', () => {
|
||||
// initialization populates file list
|
||||
expect(
|
||||
reducers(
|
||||
{ fileNames: new Set() } as State,
|
||||
{ fileNames: [] as ReadonlyArray<string> } as State,
|
||||
fileStorageDidInitialize([testFileName]),
|
||||
).fileNames,
|
||||
).toEqual(new Set([testFileName]));
|
||||
).toEqual([testFileName]);
|
||||
|
||||
// if item is not in set, add it
|
||||
expect(
|
||||
reducers(
|
||||
{ fileNames: new Set() } as State,
|
||||
{ fileNames: [] as ReadonlyArray<string> } as State,
|
||||
fileStorageDidChangeItem(testFileName),
|
||||
).fileNames,
|
||||
).toEqual(new Set([testFileName]));
|
||||
).toEqual([testFileName]);
|
||||
|
||||
// if item is already in set, there should not be duplicates
|
||||
expect(
|
||||
reducers(
|
||||
{ fileNames: new Set([testFileName]) } as State,
|
||||
{ fileNames: [testFileName] as ReadonlyArray<string> } as State,
|
||||
fileStorageDidChangeItem(testFileName),
|
||||
).fileNames,
|
||||
).toEqual(new Set([testFileName]));
|
||||
).toEqual([testFileName]);
|
||||
|
||||
// if item is in set, it should be removed
|
||||
expect(
|
||||
reducers(
|
||||
{ fileNames: new Set([testFileName]) } as State,
|
||||
{ fileNames: [testFileName] as ReadonlyArray<string> } as State,
|
||||
fileStorageDidRemoveItem(testFileName),
|
||||
).fileNames,
|
||||
).not.toContain(testFileName);
|
||||
|
||||
@@ -16,17 +16,21 @@ const isInitialized: Reducer<boolean> = (state = false, action) => {
|
||||
return state;
|
||||
};
|
||||
|
||||
const fileNames: Reducer<Set<string>> = (state = new Set(), action) => {
|
||||
const fileNames: Reducer<ReadonlyArray<string>> = (state = [], action) => {
|
||||
if (fileStorageDidInitialize.matches(action)) {
|
||||
return new Set(action.fileNames);
|
||||
return [...action.fileNames];
|
||||
}
|
||||
|
||||
if (fileStorageDidChangeItem.matches(action)) {
|
||||
return new Set([...state, action.fileName]);
|
||||
if (state.includes(action.fileName)) {
|
||||
return state;
|
||||
}
|
||||
|
||||
return [...state, action.fileName];
|
||||
}
|
||||
|
||||
if (fileStorageDidRemoveItem.matches(action)) {
|
||||
return new Set([...state].filter((value) => value !== action.fileName));
|
||||
return [...state].filter((value) => value !== action.fileName);
|
||||
}
|
||||
|
||||
return state;
|
||||
|
||||
Reference in New Issue
Block a user