[Fleet] Fix history block to support hash param (#141081)

This commit is contained in:
Nicolas Chaulet 2022-09-20 10:30:41 -04:00 committed by GitHub
parent 01b604eeb6
commit 0af26e2ab9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 1 deletions

View file

@ -102,4 +102,50 @@ describe('useHistoryBlock', () => {
expect(renderer.startServices.application.navigateToUrl).not.toBeCalled();
});
});
describe('with hash params', () => {
it('should not block if not edited', () => {
const renderer = createFleetTestRendererMock();
renderer.renderHook(() => useHistoryBlock(false));
act(() => renderer.mountHistory.push('/test#/hash'));
const { location } = renderer.mountHistory;
expect(location.pathname).toBe('/test');
expect(location.hash).toBe('#/hash');
expect(renderer.startServices.overlays.openConfirm).not.toBeCalled();
});
it('should block if edited and navigate on confirm', async () => {
const renderer = createFleetTestRendererMock();
renderer.startServices.overlays.openConfirm.mockResolvedValue(true);
renderer.renderHook(() => useHistoryBlock(true));
act(() => renderer.mountHistory.push('/test#/hash'));
// needed because we have an async useEffect
await act(() => new Promise((resolve) => resolve()));
expect(renderer.startServices.overlays.openConfirm).toBeCalled();
expect(renderer.startServices.application.navigateToUrl).toBeCalledWith(
'/mock/test#/hash',
expect.anything()
);
});
it('should block if edited and not navigate on cancel', async () => {
const renderer = createFleetTestRendererMock();
renderer.startServices.overlays.openConfirm.mockResolvedValue(false);
renderer.renderHook(() => useHistoryBlock(true));
act(() => renderer.mountHistory.push('/test#/hash'));
// needed because we have an async useEffect
await act(() => new Promise((resolve) => resolve()));
expect(renderer.startServices.overlays.openConfirm).toBeCalled();
expect(renderer.startServices.application.navigateToUrl).not.toBeCalled();
});
});
});

View file

@ -35,7 +35,10 @@ export function useHistoryBlock(isEdited: boolean) {
if (confirmRes) {
unblock();
application.navigateToUrl(state.pathname + state.search, { state: state.state });
application.navigateToUrl(state.pathname + state.hash + state.search, {
state: state.state,
});
}
}
confirmAsync();