UNPKG

bonsai-analyzer

Version:
151 lines (144 loc) 5.41 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _UrlStateEncoder = _interopRequireDefault(require("../UrlStateEncoder")); jest.mock('../fetchJSON'); let MOCK_STORE; describe('UrlStateEncoder', () => { beforeEach(() => { MOCK_STORE = { dispatch: jest.fn(), getState: jest.fn(), subscribe: jest.fn() }; _UrlStateEncoder.default.instance = null; window.location.hash = ''; }); it('should return a cached instance from the factory', () => { expect(_UrlStateEncoder.default.instance).toBeNull(); const encoder = _UrlStateEncoder.default.factory(MOCK_STORE); expect(encoder).toBe(_UrlStateEncoder.default.instance); const secondEncoder = _UrlStateEncoder.default.factory(MOCK_STORE); expect(encoder).toBe(secondEncoder); }); it('should subscribe to store changes', () => { new _UrlStateEncoder.default(MOCK_STORE); expect(MOCK_STORE.subscribe).toHaveBeenCalledTimes(1); }); it('should unsubscribe when destroyed', () => { const unsub = jest.fn(); MOCK_STORE.subscribe = jest.fn(() => unsub); new _UrlStateEncoder.default(MOCK_STORE).destroy(); expect(unsub).toHaveBeenCalledTimes(1); }); describe('reading from location', () => { it('should not dispatch when there is nothing in the location hash', () => { window.location.hash = ''; new _UrlStateEncoder.default(MOCK_STORE); window.location.hash = '#'; new _UrlStateEncoder.default(MOCK_STORE); window.location.hash = '#foo=bar'; new _UrlStateEncoder.default(MOCK_STORE); expect(MOCK_STORE.dispatch).not.toHaveBeenCalled(); }); it('should dispatch when filename is in the location hash', () => { window.location.hash = '#filename=main.js'; new _UrlStateEncoder.default(MOCK_STORE); expect(MOCK_STORE.dispatch).toHaveBeenCalledWith({ type: 'pickDataPath', path: 'main.js' }); expect(MOCK_STORE.dispatch).toHaveBeenCalledWith({ type: 'requestedDataAtPath', path: 'main.js' }); expect(MOCK_STORE.dispatch).toHaveBeenCalledTimes(2); }); it('should dispatch when chunk is in the location hash', () => { window.location.hash = '#filename=main.js&chunk=1'; new _UrlStateEncoder.default(MOCK_STORE); expect(MOCK_STORE.dispatch).toHaveBeenCalledWith({ type: 'pickDataPath', path: 'main.js' }); expect(MOCK_STORE.dispatch).toHaveBeenCalledWith({ type: 'requestedDataAtPath', path: 'main.js' }); expect(MOCK_STORE.dispatch).toHaveBeenCalledWith({ type: 'onPickedChunk', chunkId: '1' }); expect(MOCK_STORE.dispatch).toHaveBeenCalledTimes(3); }); it('should dispatch when an `rm` list is in the location hash', () => { window.location.hash = '#filename=main.js&chunk=1&rm=3,5,7'; new _UrlStateEncoder.default(MOCK_STORE); expect(MOCK_STORE.dispatch).toHaveBeenCalledWith({ type: 'pickDataPath', path: 'main.js' }); expect(MOCK_STORE.dispatch).toHaveBeenCalledWith({ type: 'requestedDataAtPath', path: 'main.js' }); expect(MOCK_STORE.dispatch).toHaveBeenCalledWith({ type: 'onPickedChunk', chunkId: '1' }); expect(MOCK_STORE.dispatch).toHaveBeenCalledWith({ type: 'onRemoveModule', moduleID: '3' }); expect(MOCK_STORE.dispatch).toHaveBeenCalledWith({ type: 'onRemoveModule', moduleID: '5' }); expect(MOCK_STORE.dispatch).toHaveBeenCalledWith({ type: 'onRemoveModule', moduleID: '7' }); expect(MOCK_STORE.dispatch).toHaveBeenCalledTimes(6); }); it('should decode encoded uri segments', () => { window.location.hash = '#filename=main%20file.js'; new _UrlStateEncoder.default(MOCK_STORE); expect(MOCK_STORE.dispatch).toHaveBeenCalledWith({ type: 'pickDataPath', path: 'main file.js' }); }); }); describe('writing to location', () => { it('should write the selectedFilename', () => { const result = new _UrlStateEncoder.default(MOCK_STORE).encodeStateForHash({ selectedFilename: 'main file.js' }); expect(result).toEqual('filename=main%20file.js'); }); it('should write the selectedChunkId', () => { const result = new _UrlStateEncoder.default(MOCK_STORE).encodeStateForHash({ selectedFilename: 'main file.js', selectedChunkId: 1 }); expect(result).toEqual('filename=main%20file.js&chunk=1'); }); it('should write the blacklistedModuleIds', () => { const result = new _UrlStateEncoder.default(MOCK_STORE).encodeStateForHash({ selectedFilename: 'main file.js', selectedChunkId: 1, blacklistedModuleIds: [3, 5, 7] }); expect(result).toEqual('filename=main%20file.js&chunk=1&rm=3%2C5%2C7'); }); it('should set the location when the store updates', () => { MOCK_STORE.getState = jest.fn(() => ({ selectedFilename: 'main.js' })); new _UrlStateEncoder.default(MOCK_STORE); const callback = MOCK_STORE.subscribe.mock.calls[0][0]; expect(window.location.hash).toEqual(''); callback(); expect(window.location.hash).toEqual('#filename=main.js'); }); }); });