Example #1
0
 it('should replace the current history item if push is false', () => {
   const history = createMemoryHistory();
   const { syncToBrowser } = mkBrowser([], history);
   syncToBrowser({ view: 'foo', foo: 'bar' }, false);
   expect(history.location.pathname).toBe('/foo');
   expect(history.location.search).toBe('?foo=bar');
   expect(history.length).toBe(1);
 });
Example #2
0
 it('should push a new history item if push is true', () => {
   const history = createMemoryHistory();
   const { syncToBrowser } = mkBrowser([], history);
   syncToBrowser({ view: 'foo', foo: 'bar' }, true);
   expect(history.location.pathname).toBe('/foo');
   expect(history.location.search).toBe('?foo=bar');
   expect(history.length).toBe(2);
 });
Example #3
0
 it('should be called synchronously after a syncToBrowser push=false, with action=REPLACE', () => {
   const onBrowserChangeSpy = jest.fn();
   const { syncToBrowser, onBrowserChange } = mkBrowser([], createMemoryHistory());
   onBrowserChange(onBrowserChangeSpy);
   expect(onBrowserChangeSpy.mock.calls.length).toBe(1);
   syncToBrowser({ view: 'foo', foo: 'bar' }, false);
   expect(onBrowserChangeSpy.mock.calls.length).toBe(1 + 1);
   expect(onBrowserChangeSpy.mock.calls[onBrowserChangeSpy.mock.calls.length - 1]).toEqual([
     { view: 'foo', foo: 'bar' },
     'REPLACE'
   ]);
 });
Example #4
0
 it('should be called synchronously after a browser back, with action=POP', () => {
   const onBrowserChangeSpy = jest.fn();
   const history = createMemoryHistory();
   const { syncToBrowser, onBrowserChange } = mkBrowser([], history);
   onBrowserChange(onBrowserChangeSpy);
   syncToBrowser({ view: 'foo', foo: 'bar' }, false);
   expect(onBrowserChangeSpy.mock.calls.length).toBe(2);
   history.goBack();
   expect(onBrowserChangeSpy.mock.calls.length).toBe(2 + 1);
   expect(onBrowserChangeSpy.mock.calls[onBrowserChangeSpy.mock.calls.length - 1]).toEqual([
     { view: 'foo', foo: 'bar' },
     'POP'
   ]);
 });
export default function createHistory<T>(
  initialEntries: HistoryEntry | Readonly<HistoryEntry[]> = '/',
  matchParams: MatchShape = {},
): RouteComponentProps<T> {
  const entries = _.castArray(initialEntries);
  const history = createMemoryHistory({ initialEntries: entries as any });
  const { params = {}, isExact = true } = matchParams;

  const match = {
    params,
    isExact,
    path: entries[0],
    url: entries[0],
  } as any;

  return {
    history,
    match,
    location: history.location,
  };
}
Example #6
0
 it('should default to an empty pathname if view param is not provided', () => {
   const history = createMemoryHistory();
   const { syncToBrowser } = mkBrowser([], history);
   syncToBrowser({ foo: 'bar', view: '' }, false);
   expect(history.location.pathname).toBe('/');
 });