summaryrefslogtreecommitdiffstats
path: root/finished-application/frontend/__tests__/PleaseSignIn.test.js
blob: f516ea32770f74c6f9e6cdc219743ceed24b05f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import React from 'react';
import { mount } from 'enzyme';
import wait from 'waait';
import PleaseSignIn from '../components/PleaseSignIn';
import { MockedProvider } from 'react-apollo/test-utils';
import { fakeUser } from '../lib/testUtils';
import { CURRENT_USER_QUERY } from '../components/User';

const notSignedInMocks = [
  {
    request: { query: CURRENT_USER_QUERY },
    result: {
      data: {
        me: null,
      },
    },
  },
];

const signedInMocks = [
  {
    request: { query: CURRENT_USER_QUERY },
    result: {
      data: {
        me: {
          ...fakeUser(),
          permissions: ['VIEW_HEY'],
        },
      },
    },
  },
];
// some dummy child component
const Hey = () => <p>Hey!</p>;

describe('<PleaseSignIn />', () => {
  it('renders sign in dialog to logged out users', async () => {
    const wrapper = mount(
      <MockedProvider mocks={notSignedInMocks}>
        <PleaseSignIn allowedPermissions={[]}>
          <Hey />
        </PleaseSignIn>
      </MockedProvider>
    );
    await wait();
    wrapper.update();
    // find the error message
    expect(wrapper.text()).toContain('Please sign in before continuing!');
    // show the sign in dialog
    expect(wrapper.find('Signin').exists()).toBeTruthy();
  });

  it('renders child for correct permissions', async () => {
    const wrapper = mount(
      <MockedProvider mocks={signedInMocks}>
        <PleaseSignIn allowedPermissions={['ADMIN', 'VIEW_HEY']}>
          <Hey />
        </PleaseSignIn>
      </MockedProvider>
    );
    await wait();
    wrapper.update();
    expect(wrapper.find('Hey').exists()).toBeTruthy();
  });
  it("renders error when permissions aren't met", async () => {
    const wrapper = mount(
      <MockedProvider mocks={signedInMocks}>
        <PleaseSignIn allowedPermissions={['ADMIN']}>
          <Hey />
        </PleaseSignIn>
      </MockedProvider>
    );
    await wait();
    wrapper.update();
    // should tell you you can't access it
    expect(wrapper.text()).toContain('Insufficient Permissions');
    // it should not render the child component
    expect(wrapper.find('Hey').exists()).toBeFalsy();
  });
});