StandardAppLayout.test.tsx 8.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Andrey Azov's avatar
Andrey Azov committed
17 18 19 20
import React from 'react';
import { mount, render } from 'enzyme';
import faker from 'faker';

Andrey Azov's avatar
Andrey Azov committed
21
import StandardAppLayout from './StandardAppLayout';
22

23
import { BreakpointWidth } from 'src/global/globalConfig';
Andrey Azov's avatar
Andrey Azov committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

const MainContent = () => (
  <div className="mainContent">This is main content</div>
);
const SidebarContent = () => (
  <div className="sidebarContent">This is sidebar content</div>
);
const TopbarContent = () => (
  <div className="topbarContent">This is topbar content</div>
);

const SidebarToolstripContent = () => (
  <div className="toolstripContent">This is topbar content</div>
);

const DrawerContent = () => (
  <div className="drawerContent">This is drawer content</div>
);

43 44 45 46
const SidebarNavigation = () => (
  <div className="sidebarNavigation">This is sidebar navigation</div>
);

Andrey Azov's avatar
Andrey Azov committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
const sidebarNavigationLinks = [...Array(3)].map(() => ({
  label: faker.random.word(),
  isActive: false
}));
const activeSidebarNavLinkIndex = Math.floor(
  Math.random() * sidebarNavigationLinks.length
);
sidebarNavigationLinks[activeSidebarNavLinkIndex].isActive = true;

const minimalProps = {
  mainContent: <MainContent />,
  sidebarContent: <SidebarContent />,
  sidebarToolstripContent: <SidebarToolstripContent />,
  topbarContent: <TopbarContent />,
  isSidebarOpen: true,
62 63 64
  sidebarNavigation: <SidebarNavigation />,
  onSidebarToggle: jest.fn(),
  viewportWidth: BreakpointWidth.DESKTOP
Andrey Azov's avatar
Andrey Azov committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
};

describe('StandardAppLayout', () => {
  afterEach(() => {
    jest.resetAllMocks();
  });

  describe('rendering', () => {
    it('renders the main content in the main section', () => {
      const wrapper = render(<StandardAppLayout {...minimalProps} />);
      expect(wrapper.find('.main').find('.mainContent').length).toBe(1);
    });

    it('renders the top bar content in the top bar', () => {
      const wrapper = render(<StandardAppLayout {...minimalProps} />);
80
      expect(wrapper.find('.topbar').find('.topbarContent').length).toBe(1);
Andrey Azov's avatar
Andrey Azov committed
81 82 83 84
    });

    it('renders the sidebar content in the sidebar', () => {
      const wrapper = render(<StandardAppLayout {...minimalProps} />);
85
      expect(wrapper.find('.sidebar').find('.sidebarContent').length).toBe(1);
Andrey Azov's avatar
Andrey Azov committed
86 87
    });

88 89
    it('renders sidebar navigation in appropriate slot', () => {
      const wrapper = render(<StandardAppLayout {...minimalProps} />);
90
      expect(wrapper.find('.topbar').find('.sidebarNavigation').length).toBe(1);
91 92
    });

Andrey Azov's avatar
Andrey Azov committed
93 94 95
    it('renders sidebar toolstrip content in the sidebar toolstrip', () => {
      const wrapper = render(<StandardAppLayout {...minimalProps} />);
      expect(
96
        wrapper.find('.sidebarToolstripContent').find('.toolstripContent')
Andrey Azov's avatar
Andrey Azov committed
97 98 99 100
          .length
      ).toBe(1);
    });

Andrey Azov's avatar
Andrey Azov committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114
    it('applies correct classes to the main section depending on whether sidebar is open', () => {
      // sidebar is open; main section is narrower
      let wrapper, mainContent;
      wrapper = render(<StandardAppLayout {...minimalProps} />);
      mainContent = wrapper.find('.main');
      expect(mainContent.hasClass('mainDefault')).toBe(true);
      expect(mainContent.hasClass('mainFullWidth')).toBe(false);

      // sidebar is closed; main section is wider
      wrapper = render(
        <StandardAppLayout {...minimalProps} isSidebarOpen={false} />
      );
      mainContent = wrapper.find('.main');
      expect(mainContent.hasClass('mainDefault')).toBe(false);
115 116 117
      expect(mainContent.hasClass('mainFullWidth')).toBe(true);
    });

Andrey Azov's avatar
Andrey Azov committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    describe('with drawer', () => {
      const props = {
        ...minimalProps,
        isDrawerOpen: false,
        drawerContent: <DrawerContent />,
        onDrawerClose: jest.fn()
      };

      it('renders drawer content in the drawer', () => {
        const wrapper = render(<StandardAppLayout {...props} />);
        expect(wrapper.find('.drawer').find('.drawerContent').length).toBe(1);
      });

      it('applies correct classes to the sidebar/drawer wrapper', () => {
        let wrapper, sidebarWrapper;
        const closedSidebarProps = {
          ...props,
          isSidebarOpen: false
        };
        wrapper = render(<StandardAppLayout {...closedSidebarProps} />);
138 139 140 141
        sidebarWrapper = wrapper.find('.sidebarWrapper');
        expect(sidebarWrapper.hasClass('sidebarWrapperOpen')).toBe(false);
        expect(sidebarWrapper.hasClass('sidebarWrapperClosed')).toBe(true);
        expect(sidebarWrapper.hasClass('sidebarWrapperDrawerOpen')).toBe(false);
Andrey Azov's avatar
Andrey Azov committed
142 143 144

        const openSidebarProps = props;
        wrapper = render(<StandardAppLayout {...openSidebarProps} />);
145 146 147 148
        sidebarWrapper = wrapper.find('.sidebarWrapper');
        expect(sidebarWrapper.hasClass('sidebarWrapperOpen')).toBe(true);
        expect(sidebarWrapper.hasClass('sidebarWrapperClosed')).toBe(false);
        expect(sidebarWrapper.hasClass('sidebarWrapperDrawerOpen')).toBe(false);
Andrey Azov's avatar
Andrey Azov committed
149 150 151 152 153 154

        const openDrawerProps = {
          ...props,
          isDrawerOpen: true
        };
        wrapper = render(<StandardAppLayout {...openDrawerProps} />);
155 156 157 158
        sidebarWrapper = wrapper.find('.sidebarWrapper');
        expect(sidebarWrapper.hasClass('sidebarWrapperOpen')).toBe(true);
        expect(sidebarWrapper.hasClass('sidebarWrapperClosed')).toBe(false);
        expect(sidebarWrapper.hasClass('sidebarWrapperDrawerOpen')).toBe(true);
Andrey Azov's avatar
Andrey Azov committed
159 160 161 162 163 164 165 166 167 168 169 170
      });
    });
  });

  describe('behaviour', () => {
    const commonProps = {
      ...minimalProps,
      isDrawerOpen: false,
      drawerContent: <DrawerContent />,
      onDrawerClose: jest.fn()
    };

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    describe('sidebar navigation tabs when sidebar is closed', () => {
      const props = { ...commonProps, isSidebarOpen: false };

      test('sidebar navigation tabs are rendered for desktop viewport and larger', () => {
        const wrapper = mount(<StandardAppLayout {...props} />);
        expect(wrapper.find('.sidebarNavigation').length).toBe(1);
      });

      test('sidebar navigation tabs are not rendered for laptop viewport and smaller', () => {
        const wrapper = mount(
          <StandardAppLayout
            {...props}
            viewportWidth={BreakpointWidth.LAPTOP}
          />
        );
        expect(wrapper.find('.sidebarNavigation').length).toBe(0);
      });
    });

Andrey Azov's avatar
Andrey Azov committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    describe('with closed drawer', () => {
      const props = commonProps;

      it('calls onSidebarToggle when sidebar toggle button is clicked', () => {
        const wrapper = mount(<StandardAppLayout {...props} />);
        wrapper.find('.sidebarModeToggleChevron').simulate('click');

        expect(props.onSidebarToggle).toHaveBeenCalledTimes(1);

        // do the same for closed sidebar
        wrapper.setProps({ isSidebarOpen: false });
        wrapper.find('.sidebarModeToggleChevron').simulate('click');

        expect(props.onSidebarToggle).toHaveBeenCalledTimes(2);
        expect(props.onDrawerClose).not.toHaveBeenCalled();
      });
    });

    describe('with open drawer', () => {
      const props = { ...commonProps, isDrawerOpen: true };

      it('closes drawer when sidebar toggle button is clicked', () => {
        const wrapper = mount(<StandardAppLayout {...props} />);
        wrapper.find('.sidebarModeToggleChevron').simulate('click');

        expect(props.onDrawerClose).toHaveBeenCalledTimes(1);
        expect(props.onSidebarToggle).not.toHaveBeenCalled();
      });

      it('closes drawer when drawer close button is clicked', () => {
        const wrapper = mount(<StandardAppLayout {...props} />);
        wrapper.find('.drawerClose').simulate('click');

        expect(props.onDrawerClose).toHaveBeenCalledTimes(1);
      });

      it('closes drawer when drawer’s transparent window is clicked', () => {
        const wrapper = mount(<StandardAppLayout {...props} />);
        wrapper.find('.drawerWindow').simulate('click');

        expect(props.onDrawerClose).toHaveBeenCalledTimes(1);
      });
    });
  });
});