forked from devhatt/pet-dex-frontend
-
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.
feat: petregister tests (devhatt#280)
* feat: all tests * fix: remove unnecessary * fix: tests and component * fix: tests text
- Loading branch information
Showing
3 changed files
with
126 additions
and
5 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 |
---|---|---|
@@ -1,9 +1,123 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { render, screen } from '@testing-library/vanilla'; | ||
import { userEvent } from '@testing-library/user-event'; | ||
|
||
import afghanHound from '../../../../stories/assets/petRegisterPage/afghanHound.svg'; | ||
import akita from '../../../../stories/assets/petRegisterPage/akita.svg'; | ||
import beagle from '../../../../stories/assets/petRegisterPage/beagle.svg'; | ||
import mixedBreed from '../../../../stories/assets/petRegisterPage/mixedBreed.svg'; | ||
|
||
import PetRegisterPage from './index'; | ||
|
||
describe('PetregisterPage', () => { | ||
it('its a function', () => { | ||
expect(PetRegisterPage).toBeInstanceOf(Function); | ||
const renderPetRegisterPage = (parameters) => | ||
render(new PetRegisterPage(parameters)); | ||
|
||
const mockCards = [ | ||
{ | ||
title: 'Mixed Breed', | ||
imgSrc: mixedBreed, | ||
imgAlt: 'mixed breed', | ||
}, | ||
{ | ||
title: 'Akita', | ||
imgSrc: akita, | ||
imgAlt: 'akita', | ||
}, | ||
|
||
{ | ||
title: 'Beagle', | ||
imgSrc: beagle, | ||
imgAlt: 'beagle', | ||
}, | ||
{ | ||
title: 'Afghan Hound', | ||
imgSrc: afghanHound, | ||
imgAlt: 'afghan hound', | ||
}, | ||
]; | ||
|
||
describe('PetRegisterPage', () => { | ||
it('renders page with breeds', () => { | ||
renderPetRegisterPage({ cards: mockCards }); | ||
|
||
const cardEvidence = screen.getByLabelText(/mixed breed/i); | ||
const button = screen.getByRole('button'); | ||
|
||
expect(cardEvidence).toBeInTheDocument(); | ||
expect(button).toBeInTheDocument(); | ||
}); | ||
|
||
it('selects the pet when it is clicked', async () => { | ||
const petRegister = renderPetRegisterPage({ cards: mockCards }); | ||
const callback = vi.fn(); | ||
|
||
petRegister.listen('select:card', callback); | ||
|
||
const card = screen.getByLabelText(/mixed breed/i); | ||
await userEvent.click(card); | ||
|
||
expect(callback).toHaveBeenCalled(); | ||
expect(petRegister.activeCard).not.toBeNull(); | ||
}); | ||
|
||
it('deselects the pet when it is clicked again', async () => { | ||
const petRegister = renderPetRegisterPage({ cards: mockCards }); | ||
|
||
const callback = vi.fn(); | ||
|
||
petRegister.listen('select:card', callback); | ||
|
||
const card = screen.getByLabelText(/mixed breed/i); | ||
await userEvent.click(card); | ||
await userEvent.click(card); | ||
|
||
expect(petRegister.activeCard).toBeNull(); | ||
}); | ||
|
||
it('deselects any previously selected card and selects the clicked one', async () => { | ||
const petRegister = renderPetRegisterPage({ cards: mockCards }); | ||
|
||
const selectCard = vi.fn(); | ||
|
||
petRegister.listen('select:card', selectCard); | ||
|
||
const $afghanHoundCard = screen.getByLabelText(/afghan hound/i); | ||
const $mixedBreedCard = screen.getByLabelText(/mixed breed/i); | ||
|
||
await userEvent.click($afghanHoundCard); | ||
const mockAfghanHoundCard = petRegister.activeCard; | ||
|
||
await userEvent.click($mixedBreedCard); | ||
const mixedBreedCard = petRegister.activeCard; | ||
|
||
expect(petRegister.activeCard).not.toBe(mockAfghanHoundCard); | ||
expect(petRegister.activeCard).toBe(mixedBreedCard); | ||
}); | ||
|
||
describe('emits', () => { | ||
it('form data when every field is validated', async () => { | ||
const petRegisterPage = renderPetRegisterPage({ cards: mockCards }); | ||
const callBackEmit = vi.fn(); | ||
petRegisterPage.listen('submit', callBackEmit); | ||
|
||
const mixedBreedCard = screen.getByLabelText(/mixed breed/i); | ||
await userEvent.click(mixedBreedCard); | ||
|
||
const button = screen.getByRole('button'); | ||
await userEvent.click(button); | ||
|
||
expect(callBackEmit).toHaveBeenCalledWith('Mixed Breed'); | ||
}); | ||
|
||
it('does not happen when theres no card selected', async () => { | ||
const petRegisterPage = renderPetRegisterPage({ cards: mockCards }); | ||
const callBackEmit = vi.fn(); | ||
petRegisterPage.listen('submit', callBackEmit); | ||
|
||
const button = screen.getByRole('button'); | ||
await userEvent.click(button); | ||
|
||
expect(callBackEmit).not.toHaveBeenCalledWith('Mixed Breed'); | ||
}); | ||
}); | ||
}); |