Castform tests refactoring

This commit is contained in:
gianantoniopini
2020-12-24 10:08:17 +01:00
parent 458aab4e8d
commit d951a3f52d

View File

@ -1,33 +1,38 @@
import React from 'react'; import React from 'react';
import { render, cleanup } from '@testing-library/react'; import { render, cleanup } from '@testing-library/react';
import path from 'path'; import FirebaseStub from 'gatsby-plugin-firebase';
import fs from 'fs';
import '../../i18n/index'; import '../../i18n/index';
import Castform from '../Castform'; import Castform from '../Castform';
let data = {}; describe('Castform', () => {
let resume = {};
beforeEach(() => { beforeEach(async () => {
const filePath = path.resolve(__dirname, '../../data/initialState.json'); FirebaseStub.database().initializeData();
const file = fs.readFileSync(filePath); const resumesPath = FirebaseStub.database().resumesPath;
data = JSON.parse(file); const resumeId = FirebaseStub.database().emptyResumeId;
}); resume = (
await FirebaseStub.database()
.ref(`${resumesPath}/${resumeId}`)
.once('value')
).val();
});
afterEach(cleanup); afterEach(cleanup);
it('renders correctly', () => { it('renders correctly', () => {
const { container } = render(<Castform data={data} />); const { container } = render(<Castform data={resume} />);
expect(container).toBeTruthy(); expect(container).toBeTruthy();
expect(container).toBeInTheDocument(); expect(container).toBeInTheDocument();
}); });
describe('date of birth', () => { describe('date of birth', () => {
const birthDateLabelMatcher = /Date of birth/i; const birthDateLabelMatcher = /Date of birth/i;
it('is not shown if not provided', () => { it('is not shown if not provided', () => {
const { queryByText } = render(<Castform data={data} />); const { queryByText } = render(<Castform data={resume} />);
expect(queryByText(birthDateLabelMatcher)).toBe(null); expect(queryByText(birthDateLabelMatcher)).toBe(null);
}); });
@ -35,13 +40,14 @@ describe('date of birth', () => {
it('is shown if provided', () => { it('is shown if provided', () => {
const birthDate = new Date(1990, 0, 20); const birthDate = new Date(1990, 0, 20);
const birthDateFormatted = '20 January 1990'; const birthDateFormatted = '20 January 1990';
data.profile.birthDate = birthDate; resume.profile.birthDate = birthDate;
const { getByText } = render(<Castform data={data} />); const { getByText } = render(<Castform data={resume} />);
expect(getByText(birthDateLabelMatcher)).toBeTruthy(); expect(getByText(birthDateLabelMatcher)).toBeTruthy();
expect(getByText(birthDateLabelMatcher)).toBeInTheDocument(); expect(getByText(birthDateLabelMatcher)).toBeInTheDocument();
expect(getByText(birthDateFormatted)).toBeTruthy(); expect(getByText(birthDateFormatted)).toBeTruthy();
expect(getByText(birthDateFormatted)).toBeInTheDocument(); expect(getByText(birthDateFormatted)).toBeInTheDocument();
}); });
});
}); });