-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
131 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,10 @@ | |
import '@testing-library/jest-dom'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import testingLibrary, { configure } from '@testing-library/dom'; | ||
import testingLibrary from '@testing-library/dom'; | ||
import userEvent from '@testing-library/user-event'; | ||
import nock from 'nock'; | ||
import * as app from '../src/application.js'; | ||
import run from '../src/application.js'; | ||
|
||
const { screen, waitFor } = testingLibrary; | ||
nock.disableNetConnect(); | ||
|
@@ -16,7 +16,6 @@ beforeEach(() => { | |
const pathToFixture = path.join('__tests__', '__fixtures__', 'index.html'); | ||
const initHtml = fs.readFileSync(pathToFixture).toString(); | ||
document.body.innerHTML = initHtml; | ||
const run = app.run ? app.run : () => { } | ||
run(); | ||
|
||
elements = { | ||
|
@@ -27,20 +26,13 @@ beforeEach(() => { | |
}); | ||
|
||
test('step1', async () => { | ||
expect(app.validateName('[email protected]')).toEqual([]); | ||
expect(app.validateName('')).toEqual(['name cannot be empty']); | ||
expect(app.validateName(' ')).toEqual(['name cannot be empty']); | ||
expect(app.validateName('e')).toEqual([]); | ||
const formContainer = document.querySelector('.form-container'); | ||
expect(formContainer.querySelector('form') | ||
.querySelector('.form-group') | ||
.querySelector('input[class="form-control"]')).not.toEqual(null); | ||
}); | ||
|
||
test('step2', async () => { | ||
expect(app.validateEmail('[email protected]')).toEqual([]); | ||
expect(app.validateEmail(' @mail.com')).toEqual(['invalid email']); | ||
expect(app.validateEmail('hhhhh @ g m a i l . c o m')).toEqual(['invalid email']); | ||
expect(app.validateEmail('s@s')).toEqual([]); | ||
}); | ||
|
||
test('step3', async () => { | ||
await userEvent.type(elements.nameInput, 'Petya'); | ||
await userEvent.type(elements.emailInput, 'wrong-email'); | ||
expect(elements.submit).toBeDisabled(); | ||
|
@@ -58,7 +50,7 @@ test('step3', async () => { | |
expect(elements.submit).not.toBeDisabled(); | ||
}); | ||
|
||
test('step4', async () => { | ||
test('step3', async () => { | ||
await userEvent.clear(elements.nameInput); | ||
await userEvent.clear(elements.emailInput); | ||
await userEvent.type(elements.nameInput, 'Petya'); | ||
|
@@ -81,7 +73,7 @@ test('step4', async () => { | |
}); | ||
|
||
|
||
test('step5', async () => { | ||
test('step4', async () => { | ||
let scope = nock('http://localhost') | ||
.post('/users') | ||
.reply(200, { | ||
|
@@ -104,7 +96,6 @@ test('step5', async () => { | |
const pathToFixture = path.join('__tests__', '__fixtures__', 'index.html'); | ||
const initHtml = fs.readFileSync(pathToFixture).toString(); | ||
document.body.innerHTML = initHtml; | ||
const run = app.run ? app.run : () => { } | ||
run(); | ||
|
||
elements = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import _ from 'lodash'; | ||
import onChange from 'on-change'; | ||
import axios from 'axios'; | ||
|
||
export const validateName = (name) => (name.trim().length ? [] : ['name cannot be empty']); | ||
export const validateEmail = (email) => (/\w+@\w+/.test(email) ? [] : ['invalid email']); | ||
const validateField = (fieldname, data) => (fieldname === 'name' ? validateName(data) : validateEmail(data)); | ||
|
||
export default () => { | ||
const formHTML = ` | ||
<form id="registrationForm"> | ||
<div class="form-group"> | ||
<label for="inputName">Name</label> | ||
<input type="text" class="form-control" id="inputName" placeholder="Введите ваше имя" name="name" required> | ||
</div> | ||
<div class="form-group"> | ||
<label for="inputEmail">Email</label> | ||
<input type="text" class="form-control" id="inputEmail" placeholder="Введите email" | ||
name="email" required> | ||
</div> | ||
<input type="submit" value="Submit" class="btn btn-primary" disabled> | ||
</form>`; | ||
|
||
const formContainer = document.querySelector('.form-container'); | ||
formContainer.innerHTML = formHTML; | ||
|
||
const state = { | ||
errors: { | ||
name: [], | ||
email: [], | ||
}, | ||
values: { | ||
name: '', | ||
email: '', | ||
}, | ||
}; | ||
|
||
const form = document.querySelector('form'); | ||
const submit = document.querySelector('[type="submit"]'); | ||
|
||
const hasErrors = () => (_.values(state.errors).reduce((acc, curr) => (curr.length > 0 | ||
? acc.concat(curr) | ||
: acc), []) | ||
.length > 0); | ||
|
||
const watchedState = onChange(state, (path) => { | ||
const selector = path.split('.')[1]; | ||
const input = document.querySelector(`[name=${selector}]`); | ||
const isFieldValid = validateField(selector, state.values[selector]).length === 0; | ||
if (!isFieldValid) { | ||
input.classList.remove('is-valid'); | ||
input.classList.add('is-invalid'); | ||
} else { | ||
input.classList.remove('is-invalid'); | ||
input.classList.add('is-valid'); | ||
} | ||
submit.disabled = hasErrors(state); | ||
}); | ||
|
||
form.addEventListener('input', (e) => { | ||
e.preventDefault(); | ||
const targetName = e.target.name; | ||
const targetData = new FormData(form).get(targetName); | ||
watchedState.values[targetName] = targetData; | ||
watchedState.errors[targetName] = (validateField(targetName, targetData)); | ||
}); | ||
|
||
form.addEventListener('submit', (e) => { | ||
e.preventDefault(); | ||
axios.post('/users', state.values) | ||
.then((response) => { | ||
document.body.innerHTML = `<p>${response.data.message}</p>`; | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
}); | ||
}); | ||
}; |