UNCLASSIFIED

Commit 3d0c7841 authored by graham.smith's avatar graham.smith
Browse files

fix tests

parent 8f3e85f9
...@@ -17,6 +17,7 @@ describe("TrainingDetails", () => { ...@@ -17,6 +17,7 @@ describe("TrainingDetails", () => {
trainingId: 42, trainingId: 42,
}, },
}; };
const mockCourse = { instructors: [] };
const propsData = { const propsData = {
trainingCourse: { trainingCourse: {
id: 1, id: 1,
...@@ -29,6 +30,10 @@ describe("TrainingDetails", () => { ...@@ -29,6 +30,10 @@ describe("TrainingDetails", () => {
jest.resetAllMocks(); jest.resetAllMocks();
}); });
it("should fetch debounced", async () => { it("should fetch debounced", async () => {
TrainingService.getCourse = jest.fn().mockResolvedValue(mockCourse);
TrainingService.getCourseRegistrationsById = jest
.fn()
.mockResolvedValue({});
TrainingDetails.fetchData = jest.fn(); TrainingDetails.fetchData = jest.fn();
const wrapper = shallowMount(TrainingDetails, { const wrapper = shallowMount(TrainingDetails, {
mocks: { mocks: {
...@@ -51,10 +56,12 @@ describe("TrainingDetails", () => { ...@@ -51,10 +56,12 @@ describe("TrainingDetails", () => {
vuetify, vuetify,
}); });
expect(wrapper.vm.fetchingData).toBe(false); expect(wrapper.vm.fetchingData).toBe(false);
await flushPromises();
wrapper.vm.fetchDebounced(); wrapper.vm.fetchDebounced();
expect(wrapper.vm.fetchingData).toBe(true); expect(wrapper.vm.fetchingData).toBe(true);
}); });
it("should fetchStudent data", async () => { it("should fetchStudent data", async () => {
TrainingService.getCourse = jest.fn().mockResolvedValue(mockCourse);
TrainingService.getCourseRegistrationsById = jest.fn().mockResolvedValue({ TrainingService.getCourseRegistrationsById = jest.fn().mockResolvedValue({
meta: { total: 2 }, meta: { total: 2 },
registrations: [ registrations: [
...@@ -68,7 +75,6 @@ describe("TrainingDetails", () => { ...@@ -68,7 +75,6 @@ describe("TrainingDetails", () => {
{}, {},
], ],
}); });
TrainingDetails.courseDates = jest.fn().mockReturnValue([]);
const wrapper = shallowMount(TrainingDetails, { const wrapper = shallowMount(TrainingDetails, {
mocks: { mocks: {
$route, $route,
...@@ -93,13 +99,13 @@ describe("TrainingDetails", () => { ...@@ -93,13 +99,13 @@ describe("TrainingDetails", () => {
}); });
await wrapper.vm.fetchStudentData(); await wrapper.vm.fetchStudentData();
await flushPromises(); await flushPromises();
expect(TrainingService.getCourseRegistrationsById).toBeCalledTimes(1); expect(TrainingService.getCourseRegistrationsById).toBeCalledTimes(2);
}); });
it("should fetchStudent data with an empty string", async () => { it("should fetchStudent data with an empty string", async () => {
TrainingService.getCourseRegistrationsById = jest TrainingService.getCourseRegistrationsById = jest
.fn() .fn()
.mockResolvedValue({}); .mockResolvedValue({});
TrainingDetails.courseDates = jest.fn().mockReturnValue([]); TrainingService.getCourse = jest.fn().mockResolvedValue(mockCourse);
const wrapper = shallowMount(TrainingDetails, { const wrapper = shallowMount(TrainingDetails, {
mocks: { mocks: {
$route, $route,
...@@ -124,14 +130,14 @@ describe("TrainingDetails", () => { ...@@ -124,14 +130,14 @@ describe("TrainingDetails", () => {
}); });
await wrapper.vm.fetchStudentData(); await wrapper.vm.fetchStudentData();
await flushPromises(); await flushPromises();
expect(TrainingService.getCourseRegistrationsById).toBeCalledTimes(1); expect(TrainingService.getCourseRegistrationsById).toBeCalledTimes(2);
expect(wrapper.vm.listItems).toStrictEqual([]); expect(wrapper.vm.listItems).toStrictEqual([]);
}); });
it("should throw error in fetchStudent", async () => { it("should throw error in fetchStudent", async () => {
TrainingService.getCourse = jest.fn().mockResolvedValue(mockCourse);
TrainingService.getCourseRegistrationsById = jest TrainingService.getCourseRegistrationsById = jest
.fn() .fn()
.mockRejectedValue("mock error"); .mockRejectedValue("mock error");
TrainingDetails.courseDates = jest.fn().mockReturnValue([]);
const wrapper = shallowMount(TrainingDetails, { const wrapper = shallowMount(TrainingDetails, {
mocks: { mocks: {
$route, $route,
...@@ -154,6 +160,7 @@ describe("TrainingDetails", () => { ...@@ -154,6 +160,7 @@ describe("TrainingDetails", () => {
localVue, localVue,
vuetify, vuetify,
}); });
await wrapper.vm.fetchStudentData(); await wrapper.vm.fetchStudentData();
await flushPromises(); await flushPromises();
expect(wrapper.vm.$store.commit).toHaveBeenCalledWith( expect(wrapper.vm.$store.commit).toHaveBeenCalledWith(
...@@ -166,6 +173,7 @@ describe("TrainingDetails", () => { ...@@ -166,6 +173,7 @@ describe("TrainingDetails", () => {
); );
}); });
it("should add student", async () => { it("should add student", async () => {
TrainingService.getCourse = jest.fn().mockResolvedValue(mockCourse);
TrainingService.getCourseRegistrationsById = jest TrainingService.getCourseRegistrationsById = jest
.fn() .fn()
.mockResolvedValue({ registrations: [] }); .mockResolvedValue({ registrations: [] });
...@@ -196,6 +204,8 @@ describe("TrainingDetails", () => { ...@@ -196,6 +204,8 @@ describe("TrainingDetails", () => {
expect(TrainingService.addStudents).toHaveBeenCalledTimes(1); expect(TrainingService.addStudents).toHaveBeenCalledTimes(1);
}); });
it("addStudent should catch error", async () => { it("addStudent should catch error", async () => {
console.error = jest.fn(); // mock console.error when expecting error messages
TrainingService.getCourse = jest.fn().mockResolvedValue(mockCourse);
TrainingService.addStudents = jest.fn().mockRejectedValue("error"); TrainingService.addStudents = jest.fn().mockRejectedValue("error");
const wrapper = shallowMount(TrainingDetails, { const wrapper = shallowMount(TrainingDetails, {
mocks: { mocks: {
...@@ -218,6 +228,7 @@ describe("TrainingDetails", () => { ...@@ -218,6 +228,7 @@ describe("TrainingDetails", () => {
); );
}); });
it("should cancel add student", async () => { it("should cancel add student", async () => {
TrainingService.getCourse = jest.fn().mockResolvedValue(mockCourse);
TrainingService.getCourseRegistrationsById = jest TrainingService.getCourseRegistrationsById = jest
.fn() .fn()
.mockResolvedValue({ registrations: [] }); .mockResolvedValue({ registrations: [] });
...@@ -248,7 +259,7 @@ describe("TrainingDetails", () => { ...@@ -248,7 +259,7 @@ describe("TrainingDetails", () => {
}); });
it("should not show error by default", () => { it("should not show error by default", () => {
TrainingService.getCourse = jest.fn().mockResolvedValue({}); TrainingService.getCourse = jest.fn().mockResolvedValue(mockCourse);
// render the component // render the component
const wrapper = shallowMount(TrainingDetails, { const wrapper = shallowMount(TrainingDetails, {
mocks: { mocks: {
...@@ -273,7 +284,7 @@ describe("TrainingDetails", () => { ...@@ -273,7 +284,7 @@ describe("TrainingDetails", () => {
}); });
it("should set training course snapshot and isEditingCourse on btnPushEditPoc", () => { it("should set training course snapshot and isEditingCourse on btnPushEditPoc", () => {
TrainingService.getCourse = jest.fn().mockResolvedValue({}); TrainingService.getCourse = jest.fn().mockResolvedValue(mockCourse);
// render the component // render the component
const wrapper = shallowMount(TrainingDetails, { const wrapper = shallowMount(TrainingDetails, {
mocks: { mocks: {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment