forked from AdaGold/exquisite-react
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathPlayerSubmissionForm.test.js
91 lines (75 loc) · 2.54 KB
/
PlayerSubmissionForm.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import React from 'react';
import '@testing-library/jest-dom/extend-expect';
import { render, screen, cleanup } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import PlayerSubmissionForm from './PlayerSubmissionForm';
import { FIELDS } from './Game';
describe('Wave 1: PlayerSubmissionForm', () => {
// Arrange
test('renders with the proper input fields and a submit button', () => {
// Act
render(<PlayerSubmissionForm
index={1}
sendSubmission={() => { }}
fields={FIELDS}
/>);
const inputFields = FIELDS.filter((item) => typeof item !== 'string');
// Assert
inputFields.forEach(field => {
const regex = new RegExp('^' + field.key + '$', 'i');
const inputField = screen.getByTestId(regex);
expect(inputField).toBeInTheDocument();
});
});
test('you can enter text in each field', () => {
// Act
render(<PlayerSubmissionForm
index={1}
sendSubmission={() => { }}
fields={FIELDS}
/>);
const inputFields = FIELDS.filter((item) => typeof item !== 'string');
let index = 0;
const letters = 'abcdefghijklmnopqrstuvwxyz';
// Assert
inputFields.forEach(async field => {
// Find the input field
const regex = new RegExp('^' + field.key + '$', 'i');
const inputField = screen.getByTestId(regex)
// Type in that input field
userEvent.type(inputField, letters.charAt(index));
// assert that the field now has the current letter
expect(inputField).toHaveValue(letters.charAt(index));
index += 1;
});
});
test('clicking on the button with text "Submit Line" the form will call the callback function', () => {
// Arrange
const callBackFunction = jest.fn();
render(<PlayerSubmissionForm
index={1}
sendSubmission={callBackFunction}
fields={FIELDS}
/>);
// Act
const submitBtn = screen.getByText(/Submit Line/i);
userEvent.click(submitBtn);
// Assert
expect(callBackFunction).toHaveBeenCalled();
});
test('the index prop determines which player\'s turn it is', () => {
for (let index = 1; index < 5; index += 1) {
// Arrange-Act
render(<PlayerSubmissionForm
index={index}
sendSubmission={() => { } }
fields={FIELDS}
/>);
// Assert
let playerText = `Player Submission Form for Player #${ index }`
expect(screen.getByText(new RegExp(playerText, 'i'))).toBeInTheDocument();
// Clear the dom prior to next loop iteration
cleanup();
}
});
});