Skip to content

Commit b88cce4

Browse files
evanyan13Evan YanMudaafi
authored
[NOCI] Update redirect structure (#162)
* Update redirect structure * Mock onRedirect * update types * Update spec/components/CioPlpGrid/CioPlpGrid.test.jsx Co-authored-by: Ahmad Mudaafi' <ahmad.mudaafi@constructor.io> --------- Co-authored-by: Evan Yan <evan.yan@constructor.io> Co-authored-by: mudaafi <ahmad.mudaafi@constructor.io>
1 parent fec6406 commit b88cce4

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

spec/components/CioPlpGrid/CioPlpGrid.test.jsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,40 @@ describe('Testing Component: CioPlpGrid', () => {
198198
});
199199
});
200200

201+
it('Should render redirect data when a search redirect response is received', async () => {
202+
// Mock redirect data
203+
const mockRedirectData = {
204+
redirect: {
205+
data: {
206+
url: 'https://example.com/redirect-url',
207+
},
208+
},
209+
};
210+
211+
const mockOnRedirect = jest.fn();
212+
jest.spyOn(require('../../../src/hooks/useCioPlpContext'), 'useCioPlpContext').mockReturnValue({
213+
callbacks: {
214+
onRedirect: mockOnRedirect,
215+
},
216+
});
217+
218+
useCioPlp.mockReturnValue({
219+
status: RequestStatus.SUCCESS,
220+
isSearchPage: true,
221+
data: mockRedirectData,
222+
});
223+
224+
render(
225+
<CioPlp apiKey={DEMO_API_KEY}>
226+
<CioPlpGrid />
227+
</CioPlp>,
228+
);
229+
230+
await waitFor(() => {
231+
expect(mockOnRedirect).toHaveBeenCalledWith('https://example.com/redirect-url');
232+
});
233+
});
234+
201235
it('Should render results with data attributes when data is fetched for Browse', async () => {
202236
useRequestConfigs.mockImplementation(() => ({
203237
getRequestConfigs: () => ({ filterName: 'group_id', filterValue: '1030' }),

src/components/CioPlpGrid/CioPlpGrid.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export default function CioPlpGrid(props: CioPlpGridWithRenderProps) {
8484
} = useCioPlpContext();
8585

8686
if (isPlpSearchDataRedirect(data)) {
87-
const redirectUrl = data.redirect.redirect.data.url;
87+
const redirectUrl = data.redirect.data.url;
8888

8989
if (onRedirect) {
9090
onRedirect(redirectUrl);

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export interface PlpSearchDataRedirect {
7272
resultId: string;
7373
request: SearchRequestType;
7474
rawApiResponse: SearchResponse;
75-
redirect: Redirect;
75+
redirect: Redirect['redirect'];
7676
}
7777

7878
export interface PlpSearchResponse {

src/utils/transformers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export function transformSearchResponse(res: SearchResponse): Nullable<PlpSearch
229229
return {
230230
resultId,
231231
request,
232-
redirect: response.redirect as Redirect,
232+
redirect: response.redirect as Redirect['redirect'],
233233
rawApiResponse: res,
234234
} as PlpSearchDataRedirect; // Type override due to partials in client-js
235235
}

0 commit comments

Comments
 (0)