From efc7c5bdf3054a43e25cbe551809f829cd0fe322 Mon Sep 17 00:00:00 2001 From: Graham Smith <gsmith@skye.local> Date: Wed, 15 Jul 2020 09:50:14 -0400 Subject: [PATCH] updating tests --- tests/unit/components/Breadcrumb.spec.js | 58 ++++++++++++++++++++++++ tests/unit/router/index.spec.js | 42 ++++++++++++----- tests/unit/views/ADCE.spec.js | 36 +++++++++++++++ tests/unit/views/ContactUs.spec.js | 31 +++++++++++++ 4 files changed, 156 insertions(+), 11 deletions(-) create mode 100644 tests/unit/components/Breadcrumb.spec.js create mode 100644 tests/unit/views/ADCE.spec.js create mode 100644 tests/unit/views/ContactUs.spec.js diff --git a/tests/unit/components/Breadcrumb.spec.js b/tests/unit/components/Breadcrumb.spec.js new file mode 100644 index 00000000..974d352f --- /dev/null +++ b/tests/unit/components/Breadcrumb.spec.js @@ -0,0 +1,58 @@ +import { shallowMount, createLocalVue } from "@vue/test-utils"; +import { BootstrapVue, IconsPlugin } from "bootstrap-vue"; +import Breadcrumb from "@/components/Breadcrumb.vue"; +import VueRouter from "vue-router"; +import Router from "@/router"; + +// vue-easy-lightbox doesn't play nice in tests, so we have to mock it +jest.mock("vue-easy-lightbox", () => { + return { + install: jest.fn() + }; +}); + +const localVue = createLocalVue(); +localVue.use(VueRouter); +localVue.use(BootstrapVue); +localVue.use(IconsPlugin); + +describe("Breadcrumb.vue", () => { + let wrapper; + beforeEach(() => { + wrapper = shallowMount(Breadcrumb, { localVue, router: Router }); + }); + afterEach(() => { + jest.clearAllMocks(); + }); + + it("should compute crumbs", async () => { + window.scrollTo = jest.fn(); + await Router.push("/services"); + expect(wrapper.vm.crumbs).toStrictEqual([ + { path: "", to: "/", text: "Home" }, + { + path: "/services", + to: "/services", + text: "Services", + active: true + } + ]); + + await Router.push("/products/abms-adce"); + expect(wrapper.vm.crumbs).toStrictEqual([ + { path: "", to: "/", text: "Home" }, + { path: "/products", to: "/products", text: "Products" }, + { + path: "/products/abms-adce", + to: "/products/abms-adce", + text: "The Party Bus", + active: true + } + ]); + + await Router.push("/mock-path"); + expect(wrapper.vm.crumbs).toStrictEqual([ + { path: "", to: "/", text: "Home", active: true } + ]); + }); +}); diff --git a/tests/unit/router/index.spec.js b/tests/unit/router/index.spec.js index 668ba393..cd8bbeea 100644 --- a/tests/unit/router/index.spec.js +++ b/tests/unit/router/index.spec.js @@ -34,23 +34,43 @@ describe("router", () => { expect(router.currentRoute.path).toBe("/"); }); - it("should route to /services", async () => { - router.push("/services"); - await wrapper.vm.$nextTick(); + it("should do routing", async () => { + await router.push("/services"); expect(router.currentRoute.name).toBe("Services"); expect(router.currentRoute.path).toBe("/services"); - }); - it("should route to /who-we-are", async () => { - router.push("/who-we-are"); - await wrapper.vm.$nextTick(); + await router.push({ path: "/who-we-are" }); expect(router.currentRoute.name).toBe("WhoWeAre"); expect(router.currentRoute.path).toBe("/who-we-are"); - }); - it("should route to /products", async () => { - router.push("/products"); - await wrapper.vm.$nextTick(); + + await router.push("/products"); expect(router.currentRoute.name).toBe("Products"); expect(router.currentRoute.path).toBe("/products"); + + await router.push("/contact-us"); + expect(router.currentRoute.name).toBe("ContactUs"); + expect(router.currentRoute.path).toBe("/contact-us"); + + await router.push("/products/abms-adce"); + expect(router.currentRoute.name).toBe("ADCE"); + expect(router.currentRoute.path).toBe("/products/abms-adce"); + }); + + it("should respect savedPosition", async () => { + const savedPosition = { x: 42, y: 24 }; + const result = await Router.options.scrollBehavior({}, {}, savedPosition); + expect(result).toStrictEqual(savedPosition); + }); + + it("should respect hash", async () => { + const to = { hash: "#test-hash" }; + const result = await Router.options.scrollBehavior(to); + expect(result).toStrictEqual({ selector: to.hash }); + }); + + it("should default scroll to 0,0", async () => { + const to = {}; + const result = await Router.options.scrollBehavior(to); + expect(result).toStrictEqual({ x: 0, y: 0 }); }); }); diff --git a/tests/unit/views/ADCE.spec.js b/tests/unit/views/ADCE.spec.js new file mode 100644 index 00000000..78ba62bd --- /dev/null +++ b/tests/unit/views/ADCE.spec.js @@ -0,0 +1,36 @@ +import { shallowMount, RouterLinkStub } from "@vue/test-utils"; +import ADCE from "@/views/ADCE.vue"; + +// vue-easy-lightbox doesn't play nice in tests, so we have to mock it +jest.mock("vue-easy-lightbox", () => { + return { + install: jest.fn() + }; +}); + +describe("ADCE.vue", () => { + let wrapper; + beforeEach(() => { + wrapper = shallowMount(ADCE, { + stubs: { + RouterLink: RouterLinkStub + } + }); + }); + afterEach(() => { + jest.clearAllMocks(); + }); + + it("should show lightbox", () => { + expect(wrapper.vm.lightbox.visible).toBe(false); + expect(wrapper.vm.lightbox.index).toBe(0); + wrapper.vm.showImg(1); + expect(wrapper.vm.lightbox.visible).toBe(true); + expect(wrapper.vm.lightbox.index).toBe(1); + }); + it("should show lightbox", () => { + wrapper.vm.showImg(0); + wrapper.vm.handleHide(); + expect(wrapper.vm.lightbox.visible).toBe(false); + }); +}); diff --git a/tests/unit/views/ContactUs.spec.js b/tests/unit/views/ContactUs.spec.js new file mode 100644 index 00000000..01b0cf7c --- /dev/null +++ b/tests/unit/views/ContactUs.spec.js @@ -0,0 +1,31 @@ +import { shallowMount, RouterLinkStub } from "@vue/test-utils"; +import { iframeResizer } from "iframe-resizer"; +import ContactUs from "@/views/ContactUs.vue"; + +describe("ContactUs.vue", () => { + let wrapper; + beforeEach(() => { + process.env.BASE_URL = "test"; + wrapper = shallowMount(ContactUs, { + stubs: { + RouterLink: RouterLinkStub + }, + directives: { + resize: { + bind: function(el, { value = {} }) { + el.addEventListener("load", () => iframeResizer(value, el)); + } + } + } + }); + }); + afterEach(() => { + jest.clearAllMocks(); + }); + + it("should respect BASE_URL", () => { + expect(wrapper.find("iframe").attributes("src")).toBe( + "test/static/p1-intake-form.html" + ); + }); +}); -- GitLab