-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent warehouses fetch when no channels in variant details (#4712)
* Prevent warehouses fetch when no channels * Add changeset * useAllwarehouses hook with tests
- Loading branch information
Showing
4 changed files
with
100 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
Prevent unnecessary warehouses fetch in product variant details when variant has not channel assigned |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/products/views/ProductVariant/useAllWarehouses.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { ChannelPriceData } from "@dashboard/channels/utils"; | ||
import { useFetchAllWarehouses } from "@dashboard/hooks/useFetchAllWarehouse"; | ||
|
||
import { useAllWarehouses } from "./useAllWarehouses"; | ||
|
||
jest.mock("@dashboard/hooks/useFetchAllWarehouse"); | ||
|
||
describe("useAllWarehouses", () => { | ||
it("should skip query when no channels and return empty array", () => { | ||
// Arrange | ||
const mockedUseFetchAllWarehouses = jest.fn(); | ||
(useFetchAllWarehouses as jest.Mock).mockImplementation( | ||
mockedUseFetchAllWarehouses, | ||
); | ||
|
||
// Act | ||
const result = useAllWarehouses([]); | ||
|
||
// Assert | ||
expect(result).toEqual([]); | ||
expect(mockedUseFetchAllWarehouses).toHaveBeenCalledWith({ | ||
displayLoader: true, | ||
skip: true, | ||
variables: { | ||
filter: { | ||
channels: [], | ||
}, | ||
first: 100, | ||
}, | ||
}); | ||
}); | ||
|
||
it("should run query when channels provided", () => { | ||
// Arrange | ||
const mockedUseFetchAllWarehouses = jest.fn(() => ({ | ||
data: { | ||
warehouses: { | ||
edges: [ | ||
{ | ||
node: { | ||
id: "1", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
})); | ||
(useFetchAllWarehouses as jest.Mock).mockImplementation( | ||
mockedUseFetchAllWarehouses, | ||
); | ||
|
||
// Act | ||
const result = useAllWarehouses([ | ||
{ | ||
id: "1", | ||
}, | ||
] as ChannelPriceData[]); | ||
|
||
// Assert | ||
expect(result).toEqual([{ id: "1" }]); | ||
expect(mockedUseFetchAllWarehouses).toHaveBeenCalledWith({ | ||
displayLoader: true, | ||
skip: false, | ||
variables: { | ||
filter: { | ||
channels: ["1"], | ||
}, | ||
first: 100, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ChannelPriceData } from "@dashboard/channels/utils"; | ||
import { useFetchAllWarehouses } from "@dashboard/hooks/useFetchAllWarehouse"; | ||
import { mapEdgesToItems } from "@dashboard/utils/maps"; | ||
|
||
export const useAllWarehouses = (channels: ChannelPriceData[]) => { | ||
const channelsIds = channels.map(channel => channel.id); | ||
|
||
const warehouses = useFetchAllWarehouses({ | ||
displayLoader: true, | ||
skip: !channelsIds.length, | ||
variables: { | ||
first: 100, | ||
filter: { | ||
channels: channelsIds, | ||
}, | ||
}, | ||
}); | ||
|
||
return mapEdgesToItems(warehouses?.data?.warehouses) || []; | ||
}; |