Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 220 - Teste da página de peso do pet #252

Merged
merged 10 commits into from
Aug 11, 2024
178 changes: 175 additions & 3 deletions src/home/components/PetWeightPage/petWeightPage.spec.js
marceana marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,16 +1,188 @@
import { describe, expect, it } from 'vitest';
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import PetWeightPage from './index';
import UploadImage from '../../../components/UploadImage';
import RangeSlider from '../../../components/RangeSlider';
import TextInput from '../../../components/TextInput';
import RadioButton from '../../../components/RadioButton';
import Button from '../../../components/Button';

let instance;

vi.mock('../../../components/UploadImage', () => ({
__esModule: true,
default: vi.fn().mockImplementation(() => ({
mount: vi.fn(),
selected: {
get: vi.fn().mockReturnValue({
classList: {
add: vi.fn(),
},
}),
},
})),
}));

vi.mock('../../../components/RangeSlider', () => ({
__esModule: true,
default: vi.fn().mockImplementation(() => ({
mount: vi.fn(),
setValue: vi.fn(),
listen: vi.fn(),
selected: {
get: vi.fn().mockReturnValue({
classList: {
add: vi.fn(),
},
}),
},
})),
}));

vi.mock('../../../components/TextInput', () => ({
__esModule: true,
default: vi.fn().mockImplementation(() => ({
mount: vi.fn(),
setValue: vi.fn(),
listen: vi.fn(),
selected: {
get: vi.fn().mockReturnValue({
classList: {
add: vi.fn(),
},
}),
},
})),
}));

vi.mock('../../../components/RadioButton', () => ({
__esModule: true,
default: vi.fn().mockImplementation(() => ({
mount: vi.fn(),
isChecked: vi.fn().mockImplementation(function isChecked() {
return this === instance.radioKG;
}),
getValue: vi.fn().mockImplementation(function getValue() {
return this === instance.radioKG ? 'kg' : 'lb';
}),
selected: {
get: vi.fn().mockReturnValue({
classList: {
add: vi.fn(),
},
}),
},
})),
}));

vi.mock('../../../components/Button', () => ({
__esModule: true,
default: vi.fn().mockImplementation(() => ({
mount: vi.fn(),
listen: vi.fn(),
selected: {
get: vi.fn().mockReturnValue({
classList: {
add: vi.fn(),
},
}),
},
})),
}));

marceana marked this conversation as resolved.
Show resolved Hide resolved
const propsMock = {
petPhoto: 'https://via.placeholder.com/150',
};

describe('PetWeightPage', () => {
beforeEach(() => {
instance = new PetWeightPage(propsMock);
});

marceana marked this conversation as resolved.
Show resolved Hide resolved
afterEach(() => {
vi.clearAllMocks();
});

it('is a Function', () => {
expect(PetWeightPage).toBeInstanceOf(Function);
});

marceana marked this conversation as resolved.
Show resolved Hide resolved
it.skip('returns an object', () => {
expect(new PetWeightPage(propsMock)).toBeInstanceOf(Object);
it('returns an object', () => {
expect(instance).toBeInstanceOf(Object);
});

marceana marked this conversation as resolved.
Show resolved Hide resolved
it('initializes components correctly', () => {
expect(UploadImage).toHaveBeenCalledTimes(1);
expect(RangeSlider).toHaveBeenCalledTimes(1);
expect(TextInput).toHaveBeenCalledTimes(1);
expect(RadioButton).toHaveBeenCalledTimes(2);
expect(Button).toHaveBeenCalledTimes(1);
});

marceana marked this conversation as resolved.
Show resolved Hide resolved
it('sets the initial pet photo', () => {
expect(UploadImage).toHaveBeenCalledWith();
});
marceana marked this conversation as resolved.
Show resolved Hide resolved

it('updates weight correctly on slider change', () => {
const mockValue = 5.0;
instance.slider.listen.mock.calls[0][1](mockValue);
expect(instance.weight).toBe(mockValue);
expect(instance.input.setValue).toHaveBeenCalledWith(mockValue);
});

it('updates slider correctly on input change', () => {
const mockValue = '5.0';
instance.input.listen.mock.calls[0][1](mockValue);
expect(instance.weight).toBe(parseFloat(mockValue));
expect(instance.slider.setValue).toHaveBeenCalledWith(
parseFloat(mockValue),
);
});
marceana marked this conversation as resolved.
Show resolved Hide resolved

it('emits weight and unit on button click', () => {
const mockWeight = 5.0;
instance.weight = mockWeight;
marceana marked this conversation as resolved.
Show resolved Hide resolved
instance.radioKG.isChecked = () => true;
instance.radioKG.getValue = () => 'kg';
const emitSpy = vi.spyOn(instance, 'emit');
marceana marked this conversation as resolved.
Show resolved Hide resolved

instance.button.listen.mock.calls[0][1]();

expect(emitSpy).toHaveBeenCalledWith('weight', mockWeight, 'kg');
});
marceana marked this conversation as resolved.
Show resolved Hide resolved

it('switches weight unit correctly', () => {
instance.radioKG.isChecked = () => true;
expect(instance.weightUnit()).toBe('kg');

instance.radioKG.isChecked = () => false;
instance.radioLB.isChecked = () => true;
expect(instance.weightUnit()).toBe('lb');
});

marceana marked this conversation as resolved.
Show resolved Hide resolved
it('applies CSS classes correctly', () => {
expect(
instance.image.selected.get('image-preview').classList.add,
).toHaveBeenCalledWith('pet-weight-page__image');
expect(
instance.slider.selected.get('range-slider').classList.add,
).toHaveBeenCalledWith('pet-weight-page__slider');
expect(
instance.slider.selected.get('range-slider-value').classList.add,
).toHaveBeenCalledWith('pet-weight-page__value');
expect(
instance.input.selected.get('input-text').classList.add,
).toHaveBeenCalledWith('pet-weight-page__input');
expect(
instance.input.selected.get('input-text-container').classList.add,
).toHaveBeenCalledWith('pet-weight-page__input-container');
expect(
instance.radioKG.selected.get('radio-container').classList.add,
).toHaveBeenCalledWith('pet-weight-page__radio');
expect(
instance.radioLB.selected.get('radio-container').classList.add,
).toHaveBeenCalledWith('pet-weight-page__radio');
expect(
instance.button.selected.get('button').classList.add,
).toHaveBeenCalledWith('pet-weight-page__button');
});
});
marceana marked this conversation as resolved.
Show resolved Hide resolved
Loading