|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { renderHook } from '@testing-library/react-hooks'; |
| 21 | +import { useUrlTracker } from './use_url_tracker'; |
| 22 | +import { StubBrowserStorage } from 'test_utils/stub_browser_storage'; |
| 23 | +import { createMemoryHistory } from 'history'; |
| 24 | + |
| 25 | +describe('useUrlTracker', () => { |
| 26 | + const key = 'key'; |
| 27 | + let storage = new StubBrowserStorage(); |
| 28 | + let history = createMemoryHistory(); |
| 29 | + beforeEach(() => { |
| 30 | + storage = new StubBrowserStorage(); |
| 31 | + history = createMemoryHistory(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should track history changes and save them to storage', () => { |
| 35 | + expect(storage.getItem(key)).toBeNull(); |
| 36 | + const { unmount } = renderHook(() => { |
| 37 | + useUrlTracker(key, history, () => false, storage); |
| 38 | + }); |
| 39 | + expect(storage.getItem(key)).toBe('/'); |
| 40 | + history.push('/change'); |
| 41 | + expect(storage.getItem(key)).toBe('/change'); |
| 42 | + unmount(); |
| 43 | + history.push('/other-change'); |
| 44 | + expect(storage.getItem(key)).toBe('/change'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('by default should restore initial url', () => { |
| 48 | + storage.setItem(key, '/change'); |
| 49 | + renderHook(() => { |
| 50 | + useUrlTracker(key, history, undefined, storage); |
| 51 | + }); |
| 52 | + expect(history.location.pathname).toBe('/change'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should restore initial url if shouldRestoreUrl cb returns true', () => { |
| 56 | + storage.setItem(key, '/change'); |
| 57 | + renderHook(() => { |
| 58 | + useUrlTracker(key, history, () => true, storage); |
| 59 | + }); |
| 60 | + expect(history.location.pathname).toBe('/change'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should not restore initial url if shouldRestoreUrl cb returns false', () => { |
| 64 | + storage.setItem(key, '/change'); |
| 65 | + renderHook(() => { |
| 66 | + useUrlTracker(key, history, () => false, storage); |
| 67 | + }); |
| 68 | + expect(history.location.pathname).toBe('/'); |
| 69 | + }); |
| 70 | +}); |
0 commit comments