Flatten out decision tree when an inventory websocket message is processed

This commit is contained in:
mabashian
2020-09-10 16:52:12 -04:00
parent 40e4ba43ef
commit 10110643ed
2 changed files with 63 additions and 36 deletions

View File

@@ -75,38 +75,56 @@ export default function useWsInventories(
return; return;
} }
const params = parseQueryString(qsConfig, location.search);
const inventory = inventories[index]; const inventory = inventories[index];
const updatedInventory = { const updatedInventory = {
...inventory, ...inventory,
}; };
if (lastMessage.group_name === 'inventories') { if (
if (lastMessage.status === 'pending_deletion') { lastMessage.group_name === 'inventories' &&
updatedInventory.pending_deletion = true; lastMessage.status === 'deleted' &&
} else if (lastMessage.status === 'deleted') { inventories.length === 1 &&
if (inventories.length === 1) { params.page > 1
const params = parseQueryString(qsConfig, location.search); ) {
if (params.page > 1) { // We've deleted the last inventory on this page so we'll
const newParams = encodeNonDefaultQueryString( // try to navigate back to the previous page
qsConfig, const newParams = encodeNonDefaultQueryString(
replaceParams(params, { qsConfig,
page: params.page - 1, replaceParams(params, {
}) page: params.page - 1,
); })
history.push(`${location.pathname}?${newParams}`); );
} history.push(`${location.pathname}?${newParams}`);
} else { return;
fetchInventories(); }
}
} else {
return;
}
} else {
if (!['pending', 'waiting', 'running'].includes(lastMessage.status)) {
enqueueId(lastMessage.inventory_id);
return;
}
if (
lastMessage.group_name === 'inventories' &&
lastMessage.status === 'deleted'
) {
fetchInventories();
return;
}
if (
!['pending', 'waiting', 'running', 'pending_deletion'].includes(
lastMessage.status
)
) {
enqueueId(lastMessage.inventory_id);
return;
}
if (
lastMessage.group_name === 'inventories' &&
lastMessage.status === 'pending_deletion'
) {
updatedInventory.pending_deletion = true;
}
if (lastMessage.group_name !== 'inventories') {
updatedInventory.isSourceSyncRunning = true; updatedInventory.isSourceSyncRunning = true;
} }

View File

@@ -31,6 +31,10 @@ function Test({
return <TestInner inventories={syncedInventories} />; return <TestInner inventories={syncedInventories} />;
} }
const QS_CONFIG = {
defaultParams: {},
};
describe('useWsInventories hook', () => { describe('useWsInventories hook', () => {
let debug; let debug;
let wrapper; let wrapper;
@@ -41,14 +45,16 @@ describe('useWsInventories hook', () => {
afterEach(() => { afterEach(() => {
global.console.debug = debug; global.console.debug = debug;
WS.clean();
}); });
test('should return inventories list', () => { test('should return inventories list', () => {
const inventories = [{ id: 1 }]; const inventories = [{ id: 1 }];
wrapper = mountWithContexts(<Test inventories={inventories} />); wrapper = mountWithContexts(
<Test inventories={inventories} qsConfig={QS_CONFIG} />
);
expect(wrapper.find('TestInner').prop('inventories')).toEqual(inventories); expect(wrapper.find('TestInner').prop('inventories')).toEqual(inventories);
WS.clean();
}); });
test('should establish websocket connection', async () => { test('should establish websocket connection', async () => {
@@ -57,7 +63,9 @@ describe('useWsInventories hook', () => {
const inventories = [{ id: 1 }]; const inventories = [{ id: 1 }];
await act(async () => { await act(async () => {
wrapper = await mountWithContexts(<Test inventories={inventories} />); wrapper = await mountWithContexts(
<Test inventories={inventories} qsConfig={QS_CONFIG} />
);
}); });
await mockServer.connected; await mockServer.connected;
@@ -71,7 +79,6 @@ describe('useWsInventories hook', () => {
}, },
}) })
); );
WS.clean();
}); });
test('should update inventory sync status', async () => { test('should update inventory sync status', async () => {
@@ -80,7 +87,9 @@ describe('useWsInventories hook', () => {
const inventories = [{ id: 1 }]; const inventories = [{ id: 1 }];
await act(async () => { await act(async () => {
wrapper = await mountWithContexts(<Test inventories={inventories} />); wrapper = await mountWithContexts(
<Test inventories={inventories} qsConfig={QS_CONFIG} />
);
}); });
await mockServer.connected; await mockServer.connected;
@@ -108,7 +117,6 @@ describe('useWsInventories hook', () => {
expect( expect(
wrapper.find('TestInner').prop('inventories')[0].isSourceSyncRunning wrapper.find('TestInner').prop('inventories')[0].isSourceSyncRunning
).toEqual(true); ).toEqual(true);
WS.clean();
}); });
test('should fetch fresh inventory after sync runs', async () => { test('should fetch fresh inventory after sync runs', async () => {
@@ -123,6 +131,7 @@ describe('useWsInventories hook', () => {
inventories={inventories} inventories={inventories}
fetchInventories={fetchInventories} fetchInventories={fetchInventories}
fetchInventoriesById={fetchInventoriesById} fetchInventoriesById={fetchInventoriesById}
qsConfig={QS_CONFIG}
/> />
); );
}); });
@@ -139,7 +148,6 @@ describe('useWsInventories hook', () => {
}); });
expect(fetchInventoriesById).toHaveBeenCalledWith([1]); expect(fetchInventoriesById).toHaveBeenCalledWith([1]);
WS.clean();
}); });
test('should update inventory pending_deletion', async () => { test('should update inventory pending_deletion', async () => {
@@ -148,7 +156,9 @@ describe('useWsInventories hook', () => {
const inventories = [{ id: 1, pending_deletion: false }]; const inventories = [{ id: 1, pending_deletion: false }];
await act(async () => { await act(async () => {
wrapper = await mountWithContexts(<Test inventories={inventories} />); wrapper = await mountWithContexts(
<Test inventories={inventories} qsConfig={QS_CONFIG} />
);
}); });
await mockServer.connected; await mockServer.connected;
@@ -176,7 +186,6 @@ describe('useWsInventories hook', () => {
expect( expect(
wrapper.find('TestInner').prop('inventories')[0].pending_deletion wrapper.find('TestInner').prop('inventories')[0].pending_deletion
).toEqual(true); ).toEqual(true);
WS.clean();
}); });
test('should refetch inventories after an inventory is deleted', async () => { test('should refetch inventories after an inventory is deleted', async () => {
@@ -191,6 +200,7 @@ describe('useWsInventories hook', () => {
inventories={inventories} inventories={inventories}
fetchInventories={fetchInventories} fetchInventories={fetchInventories}
fetchInventoriesById={fetchInventoriesById} fetchInventoriesById={fetchInventoriesById}
qsConfig={QS_CONFIG}
/> />
); );
}); });
@@ -207,6 +217,5 @@ describe('useWsInventories hook', () => {
}); });
expect(fetchInventories).toHaveBeenCalled(); expect(fetchInventories).toHaveBeenCalled();
WS.clean();
}); });
}); });