UNPKG

box-ui-elements-mlh

Version:
147 lines (129 loc) 6.33 kB
import React from 'react'; import { mount } from 'enzyme/build'; import { MemoryRouter } from 'react-router-dom'; import { SidebarPanelsComponent as SidebarPanels } from '../SidebarPanels'; // mock lazy imports jest.mock('../SidebarUtils'); describe('elements/content-sidebar/SidebarPanels', () => { const getWrapper = ({ path = '/', ...rest } = {}) => mount( <MemoryRouter initialEntries={[path]} keyLength={0}> <SidebarPanels file={{ id: '1234' }} hasActivity hasDetails hasMetadata hasSkills hasVersions isOpen {...rest} /> </MemoryRouter>, ); describe('render', () => { test.each` path | sidebar ${'/activity'} | ${'ActivitySidebar'} ${'/activity/comments'} | ${'ActivitySidebar'} ${'/activity/comments/1234'} | ${'ActivitySidebar'} ${'/activity/tasks'} | ${'ActivitySidebar'} ${'/activity/tasks/1234'} | ${'ActivitySidebar'} ${'/activity/annotations/1234/5678'} | ${'ActivitySidebar'} ${'/activity/annotations/1234'} | ${'ActivitySidebar'} ${'/activity/versions'} | ${'VersionsSidebar'} ${'/activity/versions/1234'} | ${'VersionsSidebar'} ${'/details'} | ${'DetailsSidebar'} ${'/details/versions'} | ${'VersionsSidebar'} ${'/details/versions/1234'} | ${'VersionsSidebar'} ${'/metadata'} | ${'MetadataSidebar'} ${'/skills'} | ${'SkillsSidebar'} ${'/nonsense'} | ${'SkillsSidebar'} ${'/'} | ${'SkillsSidebar'} `('should render $sidebar given the path $path', ({ path, sidebar }) => { const wrapper = getWrapper({ path }); expect(wrapper.exists(sidebar)).toBe(true); }); test('should render nothing if the sidebar is closed', () => { const wrapper = getWrapper({ isOpen: false, }); expect(wrapper.isEmptyRender()).toBe(true); }); test('should render nothing if all sidebars are disabled', () => { const wrapper = getWrapper({ hasActivity: false, hasDetails: false, hasMetadata: false, hasSkills: false, hasVersions: false, }); expect(wrapper.isEmptyRender()).toBe(true); }); describe('activity sidebar', () => { test('should render with tasks deeplink', () => { const wrapper = getWrapper({ path: '/activity/tasks/12345' }); expect(wrapper.find('ActivitySidebar').props()).toMatchObject({ activeFeedEntryType: 'task', activeFeedEntryId: '12345', }); }); test('should render with comments deeplink', () => { const wrapper = getWrapper({ path: '/activity/comments/12345' }); expect(wrapper.find('ActivitySidebar').props()).toMatchObject({ activeFeedEntryType: 'comment', activeFeedEntryId: '12345', }); }); test('should render with versions deeplink', () => { const wrapper = getWrapper({ path: '/activity/versions/12345' }); expect(wrapper.find('VersionsSidebar').props()).toMatchObject({ versionId: '12345', }); }); test('should render with annotations deeplink', () => { const wrapper = getWrapper({ path: '/activity/annotations/12345/67890' }); expect(wrapper.find('ActivitySidebar').props()).toMatchObject({ activeFeedEntryType: 'annotation', activeFeedEntryId: '67890', }); }); test('should not pass down activeFeedEntry props with partial annotations deeplink', () => { const wrapper = getWrapper({ path: '/activity/annotations/12345' }); expect(wrapper.find('ActivitySidebar').props()).toMatchObject({ activeFeedEntryType: undefined, activeFeedEntryId: undefined, }); }); }); describe('details sidebar', () => { test('should render with versions deeplink', () => { const wrapper = getWrapper({ path: '/details/versions/12345' }); expect(wrapper.find('VersionsSidebar').props()).toMatchObject({ versionId: '12345', }); }); }); describe('first loaded behavior', () => { test('should update isInitialized state on mount', () => { const wrapper = getWrapper({ path: '/activity' }); const sidebarPanels = wrapper.find(SidebarPanels); expect(sidebarPanels.state('isInitialized')).toBe(true); }); }); }); describe('refresh()', () => { test.each([true, false])('should call the sidebars with the appropriate argument', shouldRefreshCache => { const instance = getWrapper() .find(SidebarPanels) .instance(); ['activitySidebar', 'detailsSidebar', 'metadataSidebar', 'versionsSidebar'].forEach(sidebar => { instance[sidebar] = { current: { refresh: jest.fn() } }; }); instance.refresh(shouldRefreshCache); expect(instance.activitySidebar.current.refresh).toHaveBeenCalledWith(shouldRefreshCache); expect(instance.detailsSidebar.current.refresh).toHaveBeenCalledWith(); expect(instance.metadataSidebar.current.refresh).toHaveBeenCalledWith(); expect(instance.versionsSidebar.current.refresh).toHaveBeenCalledWith(); }); }); });