import { createMemoryHistory, MemoryHistory } from 'history'; import { HistoryManager } from '../history-manager'; describe(`[web-components] ${HistoryManager.name}`, () => { let historyManager: HistoryManager; let history: MemoryHistory; const url = '/foo'; beforeEach(() => { jest.useFakeTimers(); history = createMemoryHistory(); historyManager = new HistoryManager(); historyManager.register(history); }); describe('when user navigates to a new location', () => { beforeEach(() => { history.push(url); }); test('navigates to a new location', () => { expect(history.location.pathname).toEqual(url); }); }); describe('when a second history instance is registered', () => { let history2: MemoryHistory; beforeEach(() => { history2 = createMemoryHistory(); historyManager.register(history2); }); describe('when user navigates to a new location', () => { beforeEach(() => { history.push(url); }); test('navigates to a new location in all registered histories', () => { expect(history.location.pathname).toEqual(url); expect(history2.location.pathname).toEqual(url); }); }); describe('when second history is unregistered', () => { beforeEach(() => { historyManager.unregister(history2); }); describe('when user navigates to a new location', () => { beforeEach(() => { history.push(url); }); test('navigates the first history, but does not navigate second history', () => { expect(history.location.pathname).toEqual(url); expect(history2.location.pathname).not.toEqual(url); }); }); }); describe('when a redirect is setup to go to a deeper page', () => { const urlRedirect = '/foo/bar'; beforeEach(() => { history2.listen(location => { if (location.pathname === url) { history2.replace(urlRedirect); } }); }); describe('when user navigates to a new location', () => { beforeEach(() => { history.push(url); jest.runOnlyPendingTimers(); }); test('redirects to deeper page', () => { expect(history.location.pathname).toEqual(urlRedirect); expect(history2.location.pathname).toEqual(urlRedirect); }); }); }); describe('when a redirect is setup to go to a page that is not deeper', () => { const urlRedirect = '/bar'; beforeEach(() => { history2.listen(location => { if (location.pathname === url) { history2.replace(urlRedirect); } }); }); describe('when user navigates to a new location', () => { beforeEach(() => { history.push(url); jest.runOnlyPendingTimers(); }); test('does not redirect', () => { expect(history.location.pathname).toEqual(url); expect(history2.location.pathname).toEqual(url); }); }); }); }); });