Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions final/client/src/components/__tests__/loading.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React from 'react';

import { render, cleanup } from '../../test-utils';
import { mount } from '../../enzyme';
import { cleanup } from '../../test-utils';
import Loading from '../loading';

describe('Loading', () => {
// automatically unmount and cleanup DOM after the test is finished.
afterEach(cleanup);

it('renders without error', () => {
render(<Loading />);
describe('renders without error', () => {
const wrapper = mount(<Loading />);
it('should have logo element', () => {
expect(wrapper.find('svg')).not.toBeNull();
expect(wrapper.html()).toContain('logo.svg');
});
});
});
20 changes: 16 additions & 4 deletions final/client/src/components/__tests__/login-form.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import React from 'react';

import { render, cleanup } from '../../test-utils';
import { mount } from '../../enzyme';
import { cleanup } from '../../test-utils';
import LoginForm from '../login-form';

describe('Login Form', () => {
// automatically unmount and cleanup DOM after the test is finished.
afterEach(cleanup);

it('renders without error', () => {
render(<LoginForm login={() => {}}/>);
describe('renders without error', () => {
const wrapper = mount(<LoginForm login={() => {}}/>);
it('should have heading element', () => {
expect(wrapper.find('h1')).not.toBeNull();
expect(wrapper.html()).toContain('Space Explorer');
});
it('should have email input', () => {
expect(wrapper.find('input')).not.toBeNull();
expect(wrapper.html()).toContain('Email');
});
it('should have login button', () => {
expect(wrapper.find('button')).not.toBeNull();
expect(wrapper.html()).toContain('Log in');
});
});
});
81 changes: 52 additions & 29 deletions final/client/src/pages/__tests__/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,71 @@ import {
fireEvent,
waitForElement,
} from '../../test-utils';
import { mount } from '../../enzyme';
import Login, {LOGIN_USER} from '../login';
import { cache, isLoggedInVar } from '../../cache';
import { MockedProvider, MockedResponse } from '@apollo/client/testing';
import { nextTick } from 'process';
import { act } from 'react-dom/test-utils';

// setup mocks here for unit tests
const mocks = [
{
request: {query: LOGIN_USER, variables: {email: 'a@a.a'}},
result: {
data: {
login: {
id: 'abc123',
token: 'def456',
},
},
},
},
];

const updateWrapper = async (wrapper, time = 0) => {
await act(async () => {
await new Promise((res) => setTimeout(res, time));
await wrapper.update();
});
};

describe('Login Page', () => {
// automatically unmount and cleanup DOM after the test is finished.
afterEach(cleanup);

it('renders login page', async () => {
renderApollo(<Login />);
mount(
<MockedProvider mocks={mocks}>
<Login />
</MockedProvider>
);
});

it('fires login mutation and updates cache after done', async () => {
it('triggers login event and updates cache after done', async () => {
// First check that we are not logged in
expect(isLoggedInVar()).toBeFalsy();

const mocks = [
{
request: {query: LOGIN_USER, variables: {email: 'a@a.a'}},
result: {
data: {
login: {
id: 'abc123',
token: 'def456',
},
},
},
},
];

const {getByText, getByTestId} = await renderApollo(<Login />, {
mocks,
cache,
});

fireEvent.change(getByTestId('login-input'), {
target: {value: 'a@a.a'},
});

fireEvent.click(getByText(/log in/i));

// login is done if loader is gone
await waitForElement(() => getByText(/log in/i));
// Do full DOM render with mount using Enzyme
const wrapper = mount(
<MockedProvider
mocks={mocks}
cache={cache}>
<Login />
</MockedProvider>);
await updateWrapper(wrapper);

// Find email input and put mocked email
const emailInput = wrapper.find('input');
emailInput.simulate('change', { target: {value: 'a@a.a'}});

// Find and click login button
const loginButton = wrapper.find('button');
expect(loginButton.text().includes('Log in')).toBeTruthy();
wrapper.find('form').simulate('submit');
await updateWrapper(wrapper);

// Check that we are logged in
expect(isLoggedInVar()).toBeTruthy();
});
});
3 changes: 2 additions & 1 deletion final/client/src/pages/__tests__/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
waitForElement,
} from '../../test-utils';
import Profile, { GET_MY_TRIPS } from '../profile';
import { mount, render } from '../../enzyme';

const mockLaunch = {
__typename: 'Launch',
Expand Down Expand Up @@ -43,7 +44,7 @@ describe('Profile Page', () => {
},
];

const { getByText } = renderApollo(<Profile />, { mocks });
const { getByText } = render(<Profile />, { mocks });

// if the profile renders, it will have the list of missions booked
await waitForElement(() => getByText(/test mission/i));
Expand Down