diff --git a/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.js b/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.js index 6156d80692..7797256064 100644 --- a/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.js +++ b/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.js @@ -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; } diff --git a/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.test.jsx b/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.test.jsx index f7640522d2..fa18e5b175 100644 --- a/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.test.jsx +++ b/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.test.jsx @@ -31,6 +31,10 @@ function Test({ return ; } +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(); + wrapper = mountWithContexts( + + ); 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(); + wrapper = await mountWithContexts( + + ); }); 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(); + wrapper = await mountWithContexts( + + ); }); 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(); + wrapper = await mountWithContexts( + + ); }); 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(); }); });