Castform: unit tests refactoring to reduce nesting

This commit is contained in:
gianantoniopini
2021-02-02 10:40:26 +01:00
parent f72d2639e5
commit 4634b4f0a0

View File

@ -6,47 +6,48 @@ import FirebaseStub, { DatabaseConstants } from 'gatsby-plugin-firebase';
import '../../i18n/index'; import '../../i18n/index';
import Castform from '../Castform'; import Castform from '../Castform';
describe('Castform', () => { const birthDateLabelMatcher = /Date of birth/i;
let resume = {};
beforeEach(async () => { async function setup(resumeId) {
FirebaseStub.database().initializeData(); FirebaseStub.database().initializeData();
const resumeId = DatabaseConstants.initialStateResumeId; const resume = (
resume = ( await FirebaseStub.database()
await FirebaseStub.database() .ref(`${DatabaseConstants.resumesPath}/${resumeId}`)
.ref(`${DatabaseConstants.resumesPath}/${resumeId}`) .once('value')
.once('value') ).val();
).val();
});
it('renders correctly', () => { return resume;
const { container } = render(<Castform data={resume} />); }
expect(container).toBeTruthy(); test('renders correctly', async () => {
expect(container).toBeInTheDocument(); const resume = await setup(DatabaseConstants.initialStateResumeId);
});
describe('date of birth', () => { const { container } = render(<Castform data={resume} />);
const birthDateLabelMatcher = /Date of birth/i;
it('is not shown if not provided', () => { expect(container).toBeTruthy();
render(<Castform data={resume} />); expect(container).toBeInTheDocument();
});
expect(screen.queryByText(birthDateLabelMatcher)).toBeNull();
}); test('date of birth is not shown if not provided', async () => {
const resume = await setup(DatabaseConstants.initialStateResumeId);
it('is shown if provided', () => {
const birthDate = new Date(1990, 0, 20); render(<Castform data={resume} />);
const birthDateFormatted = '20 January 1990';
resume.profile.birthDate = birthDate; expect(screen.queryByText(birthDateLabelMatcher)).toBeNull();
});
render(<Castform data={resume} />);
test('date of birth is shown if provided', async () => {
expect(screen.getByText(birthDateLabelMatcher)).toBeTruthy(); const resume = await setup(DatabaseConstants.initialStateResumeId);
expect(screen.getByText(birthDateLabelMatcher)).toBeInTheDocument();
expect(screen.getByText(birthDateFormatted)).toBeTruthy(); const birthDate = new Date(1990, 0, 20);
expect(screen.getByText(birthDateFormatted)).toBeInTheDocument(); const birthDateFormatted = '20 January 1990';
}); resume.profile.birthDate = birthDate;
});
render(<Castform data={resume} />);
expect(screen.getByText(birthDateLabelMatcher)).toBeTruthy();
expect(screen.getByText(birthDateLabelMatcher)).toBeInTheDocument();
expect(screen.getByText(birthDateFormatted)).toBeTruthy();
expect(screen.getByText(birthDateFormatted)).toBeInTheDocument();
}); });