-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update browser redirect logic and specs
- Loading branch information
Beth Shook
committed
Apr 16, 2024
1 parent
81e8fc2
commit f599851
Showing
8 changed files
with
95 additions
and
32 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
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
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
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,75 @@ | ||
import createTestServices from '../../../test/createTestServices'; | ||
import createTestStore from '../../../test/createTestStore'; | ||
import { notFound } from '../../errors/routes'; | ||
import { createRouterService } from '../../navigation/routerService'; | ||
import { AnyMatch } from '../../navigation/types'; | ||
import { AppServices, Store } from '../../types'; | ||
import { assertWindow } from '../../utils'; | ||
import { processBrowserRedirect } from './processBrowserRedirect'; | ||
import * as content from '../../content'; | ||
|
||
const mockFetch = (valueToReturn: any, error?: any) => () => new Promise((resolve, reject) => { | ||
if (error) { | ||
reject(error); | ||
} | ||
resolve({ json: () => new Promise((res) => res(valueToReturn)) }); | ||
}); | ||
|
||
describe('processBrowserRedirect', () => { | ||
let services: AppServices; | ||
let store: Store; | ||
let historyReplaceSpy: jest.SpyInstance; | ||
let fetchBackup: any; | ||
let router: ReturnType<typeof createRouterService>; | ||
let window: Window; | ||
|
||
beforeEach(() => { | ||
store = createTestStore(); | ||
window = assertWindow(); | ||
router = createRouterService(Object.values(content.routes)); | ||
services = { | ||
...createTestServices(), | ||
router, | ||
}; | ||
delete (window as any).location; | ||
|
||
window.location = { | ||
origin: 'openstax.org', | ||
} as any as Window['location']; | ||
|
||
services.history.location = { | ||
pathname: '/books/physics/pages/1-introduction301', | ||
} as any; | ||
|
||
historyReplaceSpy = jest.spyOn(services.history, 'replace') | ||
.mockImplementation(jest.fn()); | ||
|
||
fetchBackup = (globalThis as any).fetch; | ||
}); | ||
|
||
afterEach(() => { | ||
(globalThis as any).fetch = fetchBackup; | ||
}); | ||
|
||
it('calls history.replace if redirect is found', async() => { | ||
(globalThis as any).fetch = mockFetch([{ from: services.history.location.pathname, to: 'redirected' }]); | ||
|
||
const match = {route: {getUrl: jest.fn(() => 'url')}} as unknown as AnyMatch; | ||
jest.spyOn(router, 'findRoute').mockReturnValue(match); | ||
|
||
await processBrowserRedirect(services); | ||
|
||
expect(historyReplaceSpy).toHaveBeenCalledWith('redirected'); | ||
}); | ||
|
||
it('updates window.location if target is not within rex', async() => { | ||
(globalThis as any).fetch = mockFetch([{ from: services.history.location.pathname, to: '/redirected' }]); | ||
|
||
const match = {route: notFound, state: false} as unknown as AnyMatch; | ||
jest.spyOn(router, 'findRoute').mockReturnValue(match); | ||
|
||
await processBrowserRedirect(services); | ||
|
||
expect(window.location.href).toEqual('openstax.org/redirected'); | ||
}); | ||
}); |
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
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
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
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