test: refs #7100 added test to vnNotes component #1121
|
@ -0,0 +1,66 @@
|
|||
import { describe, it, expect, vi, beforeAll, afterEach, beforeEach } from 'vitest';
|
||||
import { createWrapper, axios } from 'app/test/vitest/helper';
|
||||
import VnNotes from 'src/components/ui/VnNotes.vue';
|
||||
|
||||
describe('VnNotes', () => {
|
||||
let vm;
|
||||
let spyFetch;
|
||||
let postMock;
|
||||
|
||||
beforeAll(async () => {
|
||||
vi.spyOn(axios, 'get').mockReturnValue({ data: [] });
|
||||
|
||||
vm = createWrapper(VnNotes, {
|
||||
propsData: {
|
||||
url: '/test',
|
||||
filter: { order: 'created DESC' },
|
||||
body: { name: 'Tony', lastName: 'Stark' },
|
||||
addNote: false,
|
||||
selectType: true,
|
||||
|
||||
}
|
||||
}).vm;
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
postMock = vi.spyOn(axios, 'post').mockResolvedValue({ data: {name: 'Tony', lastName: 'Stark', text: 'Test Note', observationTypeFk: 1} });
|
||||
spyFetch = vi.spyOn(vm.vnPaginateRef, 'fetch').mockImplementation(() => vi.fn());
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('insert', () => {
|
||||
it('should not call axios.post if newNote.text is empty', async () => {
|
||||
vm.newNote.text = '';
|
||||
jsegarra marked this conversation as resolved
Outdated
jsegarra
commented
Este caso no se dará, está bien incluirlo, pero para ser completo el test deberíamos probar con null Este caso no se dará, está bien incluirlo, pero para ser completo el test deberíamos probar con null
|
||||
vm.newNote.observationTypeFk = 1;
|
||||
|
||||
await vm.insert();
|
||||
|
||||
jsegarra marked this conversation as resolved
Outdated
jsegarra
commented
porque usas await? porque usas await?
jtubau
commented
Para asegurar que selectType haya cambiado correctamente antes de continuar, ya que si no lo uso, se ejecuta vm.insert() antes de que se actualice el valor y el test falla, lo mismo con el vm.insert, ya que los expect se ejecutan antes de que sus correspondientes métodos espiados sean ejecutados. Para asegurar que selectType haya cambiado correctamente antes de continuar, ya que si no lo uso, se ejecuta vm.insert() antes de que se actualice el valor y el test falla, lo mismo con el vm.insert, ya que los expect se ejecutan antes de que sus correspondientes métodos espiados sean ejecutados.
jsegarra
commented
Estoy mirando la documentación y devuelve una promesa, https://test-utils.vuejs.org/api/#setProps Estoy mirando la documentación y devuelve una promesa, https://test-utils.vuejs.org/api/#setProps
Cuando puse el comentario no vi el await
|
||||
expect(postMock).not.toHaveBeenCalled();
|
||||
expect(spyFetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not call axios.post if observationTypeFk is missing and selectType is set', async () => {
|
||||
vm.newNote.text = 'Test Note';
|
||||
vm.newNote.observationTypeFk = null;
|
||||
|
||||
await vm.insert();
|
||||
|
||||
expect(postMock).not.toHaveBeenCalled();
|
||||
expect(spyFetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should call axios.post and vnPaginateRef.fetch when newNote is valid', async () => {
|
||||
vm.newNote.text = 'Test Note';
|
||||
vm.newNote.observationTypeFk = 1;
|
||||
|
||||
const expectedBody = {...vm.$props.body, ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }};
|
||||
|
||||
await vm.insert();
|
||||
|
||||
expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedBody);
|
||||
expect(spyFetch).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Todos los test asument que selectType es true, que pasa cuando le damos valor false?
Falla