forked from t4d-classes/react-redux-typescript_09282020
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
f54ddbb
commit 6119c6d
Showing
6 changed files
with
215 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import React from 'react'; | ||
import { render, RenderResult, fireEvent } from '@testing-library/react'; | ||
|
||
import { NewCar, CarKeys } from '../models/car'; | ||
import { CarForm } from '../components/CarForm'; | ||
|
||
describe('CarViewRow testing library', () => { | ||
let car: NewCar; | ||
let textInputKeys: CarKeys[] = ['make', 'model', 'color']; | ||
let numberInputKeys: CarKeys[] = ['year', 'price']; | ||
let renderResult: RenderResult; | ||
let submitCarSpy: jest.Mock; | ||
|
||
beforeEach(() => { | ||
car = { | ||
make: 'Ford', | ||
model: 'Fusion Hybrid', | ||
year: 2020, | ||
color: 'red', | ||
price: 45000, | ||
}; | ||
|
||
submitCarSpy = jest.fn(); | ||
}); | ||
|
||
beforeEach(() => { | ||
renderResult = render( | ||
<CarForm buttonText="Add Car" onSubmitCar={submitCarSpy} />, | ||
); | ||
}); | ||
|
||
test('render CarViewRow component', () => { | ||
const { getAllByRole } = renderResult; | ||
|
||
const textboxInputs = getAllByRole('textbox'); | ||
expect(textboxInputs.length).toBe(3); | ||
textboxInputs.forEach((input: HTMLInputElement, index: number) => { | ||
const evt = { | ||
target: { | ||
type: input.type, | ||
value: car[textInputKeys[index]], | ||
name: input.name, | ||
}, | ||
}; | ||
fireEvent.change(input, evt); | ||
}); | ||
|
||
const spinbuttonInputs = getAllByRole('spinbutton'); | ||
expect(spinbuttonInputs.length).toBe(2); | ||
spinbuttonInputs.forEach((input: HTMLInputElement, index: number) => { | ||
const evt = { | ||
target: { | ||
type: input.type, | ||
value: car[numberInputKeys[index]], | ||
name: input.name, | ||
}, | ||
}; | ||
fireEvent.change(input, evt); | ||
}); | ||
|
||
const submitButton = renderResult.getByRole('button'); | ||
|
||
fireEvent.click(submitButton); | ||
expect(submitCarSpy).toHaveBeenCalledWith(car); | ||
}); | ||
}); |
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,55 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
|
||
import { Car, CarsOrder, ORDER_ASC } from '../models/car'; | ||
import { CarTable } from './CarTable'; | ||
|
||
jest.mock('./CarViewRow'); | ||
jest.mock('./CarEditRow'); | ||
|
||
describe('CarTable render', () => { | ||
let cars: Car[]; | ||
let carsOrder: CarsOrder; | ||
|
||
beforeEach(() => { | ||
carsOrder = { | ||
column: 'id', | ||
direction: ORDER_ASC, | ||
}; | ||
cars = [ | ||
{ | ||
id: 1, | ||
make: 'Ford', | ||
model: 'Fusion Hybrid', | ||
year: 2020, | ||
color: 'blue', | ||
price: 45000, | ||
}, | ||
{ | ||
id: 2, | ||
make: 'Tesla', | ||
model: 'S', | ||
year: 2019, | ||
color: 'red', | ||
price: 120000, | ||
}, | ||
]; | ||
}); | ||
|
||
test('render', () => { | ||
const { debug } = render( | ||
<CarTable | ||
cars={cars} | ||
editCarId={-1} | ||
carsOrder={carsOrder} | ||
onEditCar={() => null} | ||
onDeleteCar={() => null} | ||
onSaveCar={() => null} | ||
onCancelCar={() => null} | ||
onSortCars={() => null} | ||
/>, | ||
); | ||
|
||
debug(); | ||
}); | ||
}); |
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,3 @@ | ||
import React from 'react'; | ||
|
||
export const CarEditRow = () => <></>; |
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,3 @@ | ||
import React from 'react'; | ||
|
||
export const CarViewRow = () => <></>; |
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,56 @@ | ||
import { ChangeEvent } from 'react'; | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useForm } from './useForm'; | ||
|
||
describe('use form custom hook', () => { | ||
test('use form', () => { | ||
const { result } = renderHook(() => useForm({ firstName: '', age: NaN })); | ||
|
||
let form = result.current[0]; | ||
expect(form.firstName).toEqual(''); | ||
expect(form.age).toBeNaN(); | ||
|
||
act(() => { | ||
const e = { | ||
target: { | ||
name: 'firstName', | ||
type: 'text', | ||
value: 'B', | ||
} as HTMLInputElement, | ||
} as ChangeEvent<HTMLInputElement>; | ||
|
||
const change = result.current[1]; | ||
change(e); | ||
}); | ||
|
||
form = result.current[0]; | ||
expect(form.firstName).toEqual('B'); | ||
expect(form.age).toBeNaN(); | ||
|
||
act(() => { | ||
const e = { | ||
target: { | ||
name: 'age', | ||
type: 'number', | ||
value: '20', | ||
} as HTMLInputElement, | ||
} as ChangeEvent<HTMLInputElement>; | ||
|
||
const change = result.current[1]; | ||
change(e); | ||
}); | ||
|
||
form = result.current[0]; | ||
expect(form.firstName).toEqual('B'); | ||
expect(form.age).toEqual(20); | ||
|
||
act(() => { | ||
const resetForm = result.current[2]; | ||
resetForm(); | ||
}); | ||
|
||
form = result.current[0]; | ||
expect(form.firstName).toEqual(''); | ||
expect(form.age).toBeNaN(); | ||
}); | ||
}); |