mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-15 17:21:35 +10:00
Dashboard page: refactored setup of fetch mock, added more assertions to unit test
This commit is contained in:
@ -2,7 +2,7 @@
|
|||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
import Constants from '../constants/auth';
|
import Constants from '../constants/auth';
|
||||||
import delay from '../../utils/index';
|
import { delay } from '../../../src/utils/index';
|
||||||
|
|
||||||
const singleton = Symbol('');
|
const singleton = Symbol('');
|
||||||
const singletonEnforcer = Symbol('');
|
const singletonEnforcer = Symbol('');
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import { v4 as uuidv4 } from 'uuid';
|
|||||||
|
|
||||||
import DatabaseConstants from '../constants/database';
|
import DatabaseConstants from '../constants/database';
|
||||||
import DataSnapshot from './dataSnapshot';
|
import DataSnapshot from './dataSnapshot';
|
||||||
import delay from '../../utils/index';
|
import { delay } from '../../../src/utils/index';
|
||||||
|
|
||||||
const parsePath = (path) => {
|
const parsePath = (path) => {
|
||||||
if (!path) {
|
if (!path) {
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
import delay from './utils/index';
|
import { delay } from '../src/utils/index';
|
||||||
|
|
||||||
const Gatsby = jest.requireActual('gatsby');
|
const Gatsby = jest.requireActual('gatsby');
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +0,0 @@
|
|||||||
const delay = async (milliseconds) => {
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
||||||
};
|
|
||||||
|
|
||||||
export default delay;
|
|
||||||
@ -13,6 +13,7 @@ import FirebaseStub, { DatabaseConstants } from 'gatsby-plugin-firebase';
|
|||||||
|
|
||||||
import '../../../i18n/index';
|
import '../../../i18n/index';
|
||||||
import '../../../utils/dayjs';
|
import '../../../utils/dayjs';
|
||||||
|
import { unsplashPhotoRequestUrl, delay } from '../../../utils/index';
|
||||||
import { dataTestId as loadingScreenTestId } from '../../../components/router/LoadingScreen';
|
import { dataTestId as loadingScreenTestId } from '../../../components/router/LoadingScreen';
|
||||||
import { createResumeButtonDataTestId } from '../../../components/dashboard/CreateResume';
|
import { createResumeButtonDataTestId } from '../../../components/dashboard/CreateResume';
|
||||||
import { menuToggleDataTestIdPrefix as resumePreviewMenuToggleDataTestIdPrefix } from '../../../components/dashboard/ResumePreview';
|
import { menuToggleDataTestIdPrefix as resumePreviewMenuToggleDataTestIdPrefix } from '../../../components/dashboard/ResumePreview';
|
||||||
@ -112,8 +113,25 @@ describe('Dashboard', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('when resume is created', () => {
|
describe('when resume is created', () => {
|
||||||
|
const unsplashPhotoResponseUrl = 'https://test-url-123456789.com';
|
||||||
let nameTextBox = null;
|
let nameTextBox = null;
|
||||||
|
|
||||||
|
const setupFetchMock = () => {
|
||||||
|
fetch.resetMocks();
|
||||||
|
|
||||||
|
fetch.mockImplementationOnce(async (input) => {
|
||||||
|
await delay(100);
|
||||||
|
|
||||||
|
if (input === unsplashPhotoRequestUrl) {
|
||||||
|
return {
|
||||||
|
url: unsplashPhotoResponseUrl,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('Unsupported input.');
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const waitForModalWindowToHaveBeenClosed = async () => {
|
const waitForModalWindowToHaveBeenClosed = async () => {
|
||||||
await waitFor(() =>
|
await waitFor(() =>
|
||||||
screen.queryByRole('textbox', { name: /name/i })
|
screen.queryByRole('textbox', { name: /name/i })
|
||||||
@ -123,10 +141,9 @@ describe('Dashboard', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await setup();
|
setupFetchMock();
|
||||||
|
|
||||||
fetch.resetMocks();
|
await setup();
|
||||||
fetch.mockReturnValueOnce({ url: 'https://test-url-123456789.com' });
|
|
||||||
|
|
||||||
const dashboardCreateResumeButton = await screen.findByTestId(
|
const dashboardCreateResumeButton = await screen.findByTestId(
|
||||||
createResumeButtonDataTestId,
|
createResumeButtonDataTestId,
|
||||||
@ -235,6 +252,9 @@ describe('Dashboard', () => {
|
|||||||
).filter((resume) => resume.name === resumeName);
|
).filter((resume) => resume.name === resumeName);
|
||||||
expect(actualUserResumesFiltered).toHaveLength(1);
|
expect(actualUserResumesFiltered).toHaveLength(1);
|
||||||
const createdResume = actualUserResumesFiltered[0];
|
const createdResume = actualUserResumesFiltered[0];
|
||||||
|
expect(createdResume.id).toBeTruthy();
|
||||||
|
expect(createdResume.preview).toBeTruthy();
|
||||||
|
expect(createdResume.preview).toEqual(unsplashPhotoResponseUrl);
|
||||||
expect(createdResume.createdAt).toBeTruthy();
|
expect(createdResume.createdAt).toBeTruthy();
|
||||||
expect(createdResume.createdAt).toBeGreaterThanOrEqual(now);
|
expect(createdResume.createdAt).toBeGreaterThanOrEqual(now);
|
||||||
expect(createdResume.createdAt).toEqual(createdResume.updatedAt);
|
expect(createdResume.createdAt).toEqual(createdResume.updatedAt);
|
||||||
|
|||||||
@ -54,8 +54,11 @@ export const getFieldProps = (formik, schema, name) => ({
|
|||||||
...formik.getFieldProps(name),
|
...formik.getFieldProps(name),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const unsplashPhotoRequestUrl =
|
||||||
|
'https://source.unsplash.com/featured/400x600';
|
||||||
|
|
||||||
export const getUnsplashPhoto = async () => {
|
export const getUnsplashPhoto = async () => {
|
||||||
const response = await fetch('https://source.unsplash.com/featured/400x600');
|
const response = await fetch(unsplashPhotoRequestUrl);
|
||||||
return response.url;
|
return response.url;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -121,3 +124,7 @@ export const b64toBlob = (b64Data, contentType = '', sliceSize = 512) => {
|
|||||||
const blob = new Blob(byteArrays, { type: contentType });
|
const blob = new Blob(byteArrays, { type: contentType });
|
||||||
return blob;
|
return blob;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const delay = async (milliseconds) => {
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, milliseconds));
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user