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

View File

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