StandardAppLayout.test.tsx 7.34 KB
Newer Older
Andrey Azov's avatar
Andrey Azov committed
1
2
3
4
import React from 'react';
import { mount, render } from 'enzyme';
import faker from 'faker';

5
import StandardAppLayout, { SidebarBehaviourType } from './StandardAppLayout';
6

7
import { BreakpointWidth } from 'src/global/globalConfig';
Andrey Azov's avatar
Andrey Azov committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

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>
);

27
28
29
30
const SidebarNavigation = () => (
  <div className="sidebarNavigation">This is sidebar navigation</div>
);

Andrey Azov's avatar
Andrey Azov committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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,
46
47
48
  sidebarNavigation: <SidebarNavigation />,
  onSidebarToggle: jest.fn(),
  viewportWidth: BreakpointWidth.DESKTOP
Andrey Azov's avatar
Andrey Azov committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
};

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} />);
64
      expect(wrapper.find('.topbar').find('.topbarContent').length).toBe(1);
Andrey Azov's avatar
Andrey Azov committed
65
66
67
68
    });

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

72
73
    it('renders sidebar navigation in appropriate slot', () => {
      const wrapper = render(<StandardAppLayout {...minimalProps} />);
74
      expect(wrapper.find('.topbar').find('.sidebarNavigation').length).toBe(1);
75
76
    });

Andrey Azov's avatar
Andrey Azov committed
77
78
79
    it('renders sidebar toolstrip content in the sidebar toolstrip', () => {
      const wrapper = render(<StandardAppLayout {...minimalProps} />);
      expect(
80
        wrapper.find('.sidebarToolstripContent').find('.toolstripContent')
Andrey Azov's avatar
Andrey Azov committed
81
82
83
84
          .length
      ).toBe(1);
    });

85
86
87
88
89
90
91
92
93
94
    it('does not change main area styles when sidebar slides over it', () => {
      const props = {
        ...minimalProps,
        sidebarBehaviour: SidebarBehaviourType.SLIDEOVER
      };
      const wrapper = render(<StandardAppLayout {...props} />);
      const mainContent = wrapper.find('.main');
      expect(mainContent.hasClass('mainFullWidth')).toBe(true);
    });

Andrey Azov's avatar
Andrey Azov committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    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} />);
115
116
117
118
        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
119
120
121

        const openSidebarProps = props;
        wrapper = render(<StandardAppLayout {...openSidebarProps} />);
122
123
124
125
        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
126
127
128
129
130
131

        const openDrawerProps = {
          ...props,
          isDrawerOpen: true
        };
        wrapper = render(<StandardAppLayout {...openDrawerProps} />);
132
133
134
135
        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
136
137
138
139
140
141
142
143
144
145
146
147
      });
    });
  });

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

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    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);
      });
    });
  });
});