2019-08-25 19:34:27 +07:00
|
|
|
import React from 'react';
|
|
|
|
import { render, cleanup } from '@testing-library/react';
|
|
|
|
|
|
|
|
import { MemoryRouter } from 'react-router';
|
|
|
|
|
|
|
|
import vueMetadata from '../../../test/fixtures/metadata/vue.json';
|
|
|
|
|
|
|
|
import Version from './Version';
|
|
|
|
import { waitForElement } from '@testing-library/dom';
|
|
|
|
import ErrorBoundary from '../../App/AppError';
|
|
|
|
|
|
|
|
// :-) we mock this otherways fails on render, some weird issue on material-ui
|
2019-10-06 23:30:05 +07:00
|
|
|
jest.mock('../../muiComponents/Avatar');
|
2019-08-25 19:34:27 +07:00
|
|
|
|
2019-08-31 16:02:46 +07:00
|
|
|
// eslint-disable-next-line react/display-name
|
|
|
|
jest.mock('../../components/NotFound', () => () => <div>{'Not found'}</div>);
|
|
|
|
|
2019-08-25 19:34:27 +07:00
|
|
|
describe('test Version page', () => {
|
2019-08-31 16:02:46 +07:00
|
|
|
jest.setTimeout(40000000);
|
2019-08-25 19:34:27 +07:00
|
|
|
beforeAll(() => {
|
|
|
|
// FIXME: a better way to mock this
|
|
|
|
// @ts-ignore
|
|
|
|
global.window.VERDACCIO_API_URL = 'http://test';
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
cleanup();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
2019-08-25 22:39:15 +07:00
|
|
|
// @ts-ignore
|
|
|
|
fetch.resetMocks();
|
2019-08-25 19:34:27 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should render the version page', async () => {
|
|
|
|
const readmeText = 'test';
|
|
|
|
// @ts-ignore
|
|
|
|
fetch.mockResponses(
|
|
|
|
[[JSON.stringify(vueMetadata)], { status: 200, headers: { 'content-type': 'application/json' } }],
|
|
|
|
[[`<p align="center">${readmeText}</p>`], { status: 200, headers: { 'content-type': 'text/html' } }]
|
|
|
|
);
|
|
|
|
|
|
|
|
const { getByTestId, getByText } = render(
|
|
|
|
<ErrorBoundary>
|
|
|
|
<MemoryRouter>
|
|
|
|
<Version match={{ params: { ['package']: 'vue' } }}></Version>
|
|
|
|
</MemoryRouter>
|
|
|
|
</ErrorBoundary>
|
|
|
|
);
|
|
|
|
|
|
|
|
// first it display loading
|
|
|
|
const hasLoading = getByTestId('loading');
|
|
|
|
expect(hasLoading).toBeTruthy();
|
|
|
|
|
|
|
|
// we wait fetch response (mocked above)
|
|
|
|
await waitForElement(() => getByTestId('version-layout'));
|
|
|
|
|
|
|
|
// check whether readme was loaded
|
|
|
|
const hasReadme = getByText(readmeText);
|
|
|
|
|
|
|
|
expect(hasReadme).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should render 404 page if the resources are not found', async () => {
|
|
|
|
// @ts-ignore
|
|
|
|
fetch.mockResponses(
|
|
|
|
[[JSON.stringify({})], { status: 404, headers: { 'content-type': 'application/json' } }],
|
|
|
|
[[``], { status: 404, headers: { 'content-type': 'text/html' } }]
|
|
|
|
);
|
|
|
|
|
|
|
|
const { getByTestId, getByText } = render(
|
|
|
|
<ErrorBoundary>
|
|
|
|
<MemoryRouter>
|
|
|
|
<Version match={{ params: { ['package']: 'vue' } }}></Version>
|
|
|
|
</MemoryRouter>
|
|
|
|
</ErrorBoundary>
|
|
|
|
);
|
|
|
|
|
|
|
|
// first it display loading
|
|
|
|
const hasLoading = getByTestId('loading');
|
|
|
|
expect(hasLoading).toBeTruthy();
|
|
|
|
|
|
|
|
// we wait fetch response (mocked above)
|
2019-08-31 16:02:46 +07:00
|
|
|
await waitForElement(() => getByText('Not found'));
|
2019-08-25 19:34:27 +07:00
|
|
|
|
|
|
|
// check whether readme was loaded
|
2019-08-31 16:02:46 +07:00
|
|
|
const hasReadme = getByText('Not found');
|
2019-08-25 19:34:27 +07:00
|
|
|
|
|
|
|
expect(hasReadme).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wanna contribute? Here we some scenarios we need to test
|
|
|
|
|
|
|
|
test.todo('should test click on tabs');
|
|
|
|
test.todo('should check what is rendered int he sidebar is correct');
|
|
|
|
test.todo('should test click back home on 404');
|
|
|
|
test.todo('should test click on elements in the sidebar');
|
|
|
|
test.todo('should test other not consider scenarios');
|
|
|
|
});
|