From d0036f177f506240263a72467a29f664c57aae48 Mon Sep 17 00:00:00 2001 From: Amir Raminfar Date: Wed, 12 Jun 2019 19:40:27 -0700 Subject: [PATCH] Adds more tests and changes files (#36) --- assets/App.vue | 7 ++- .../__snapshots__/app.spec.js.snap | 14 ++--- assets/{tests => }/app.spec.js | 22 ++++++-- assets/pages/Container.spec.js | 55 +++++++++++++++++++ assets/pages/Index.spec.js | 14 +++++ .../__snapshots__/Container.spec.js.snap | 50 +++++++++++++++++ assets/pages/__snapshots__/Index.spec.js.snap | 21 +++++++ package.json | 12 ++++ 8 files changed, 178 insertions(+), 17 deletions(-) rename assets/{tests => }/__snapshots__/app.spec.js.snap (79%) rename assets/{tests => }/app.spec.js (58%) create mode 100644 assets/pages/Container.spec.js create mode 100644 assets/pages/Index.spec.js create mode 100644 assets/pages/__snapshots__/Container.spec.js.snap create mode 100644 assets/pages/__snapshots__/Index.spec.js.snap diff --git a/assets/App.vue b/assets/App.vue index 3a465073..6b2f392a 100644 --- a/assets/App.vue +++ b/assets/App.vue @@ -31,14 +31,15 @@ export default { name: "App", data() { return { - title: "Dozzle", + title: "", containers: [], showNav: false }; }, metaInfo() { return { - title: this.title + title: this.title, + titleTemplate: "%s - Dozzle" }; }, async created() { @@ -55,7 +56,7 @@ export default { methods: { async fetchContainerList() { this.containers = await (await fetch(`${BASE_PATH}/api/containers.json`)).json(); - this.title = `${this.containers.length} containers - Dozzle`; + this.title = `${this.containers.length} containers`; } }, watch: { diff --git a/assets/tests/__snapshots__/app.spec.js.snap b/assets/__snapshots__/app.spec.js.snap similarity index 79% rename from assets/tests/__snapshots__/app.spec.js.snap rename to assets/__snapshots__/app.spec.js.snap index 9c1b4818..b95ab138 100644 --- a/assets/tests/__snapshots__/app.spec.js.snap +++ b/assets/__snapshots__/app.spec.js.snap @@ -34,28 +34,22 @@ exports[` renders correctly 1`] = ` class="menu-list is-hidden-mobile" >
  • - +
    Test 1
    -
    +
  • - +
    Test 2
    -
    +
  • diff --git a/assets/tests/app.spec.js b/assets/app.spec.js similarity index 58% rename from assets/tests/app.spec.js rename to assets/app.spec.js index 0965688e..c386e616 100644 --- a/assets/tests/app.spec.js +++ b/assets/app.spec.js @@ -1,10 +1,10 @@ import fetchMock from "fetch-mock"; import EventSource from "eventsourcemock"; -import { shallowMount } from "@vue/test-utils"; -import App from "../App"; +import { shallowMount, RouterLinkStub } from "@vue/test-utils"; +import App from "./App"; describe("", () => { - const stubs = ["router-link", "router-view"]; + const stubs = { RouterLink: RouterLinkStub, "router-view": true }; beforeEach(() => { global.BASE_PATH = ""; global.EventSource = EventSource; @@ -20,7 +20,7 @@ describe("", () => { test("has right title", async () => { const wrapper = shallowMount(App, { stubs }); await fetchMock.flush(); - expect(wrapper.vm.title).toBe("2 containers - Dozzle"); + expect(wrapper.vm.title).toContain("2 containers"); }); test("renders correctly", async () => { @@ -28,4 +28,18 @@ describe("", () => { await fetchMock.flush(); expect(wrapper.element).toMatchSnapshot(); }); + + test("renders router-link correctly", async () => { + const wrapper = shallowMount(App, { stubs }); + await fetchMock.flush(); + expect(wrapper.find(RouterLinkStub).props().to).toMatchInlineSnapshot(` + Object { + "name": "container", + "params": Object { + "id": "abc", + "name": "Test 1", + }, + } + `); + }); }); diff --git a/assets/pages/Container.spec.js b/assets/pages/Container.spec.js new file mode 100644 index 00000000..1ea7eb56 --- /dev/null +++ b/assets/pages/Container.spec.js @@ -0,0 +1,55 @@ +import EventSource from "eventsourcemock"; +import { sources } from "eventsourcemock"; +import { shallowMount } from "@vue/test-utils"; +import Container from "./Container"; + +describe("", () => { + beforeEach(() => { + global.BASE_PATH = ""; + global.EventSource = EventSource; + }); + + test("is a Vue instance", async () => { + const wrapper = shallowMount(Container); + expect(wrapper.isVueInstance()).toBeTruthy(); + }); + + test("renders correctly", async () => { + const wrapper = shallowMount(Container); + expect(wrapper.element).toMatchSnapshot(); + }); + + test("should connect to EventSource", async () => { + shallowMount(Container, { + propsData: { id: "abc" } + }); + sources["/api/logs/stream?id=abc"].emitOpen(); + expect(sources["/api/logs/stream?id=abc"].readyState).toBe(1); + }); + + test("should close EventSource", async () => { + const wrapper = shallowMount(Container, { + propsData: { id: "abc" } + }); + sources["/api/logs/stream?id=abc"].emitOpen(); + wrapper.destroy(); + expect(sources["/api/logs/stream?id=abc"].readyState).toBe(2); + }); + + test("should parse messages", async () => { + const wrapper = shallowMount(Container, { + propsData: { id: "abc" } + }); + sources["/api/logs/stream?id=abc"].emitOpen(); + sources["/api/logs/stream?id=abc"].emitMessage({ data: `2019-06-13T00:55:42.459034602Z "This is a message."` }); + const [{ dateRelative, ...other }, _] = wrapper.vm.messages; + + expect(other).toMatchInlineSnapshot(` + Object { + "date": 2019-06-13T00:55:42.459Z, + "key": 0, + "message": " \\"This is a message.\\"", + } + `); + }); +}); diff --git a/assets/pages/Index.spec.js b/assets/pages/Index.spec.js new file mode 100644 index 00000000..8dc0ddd2 --- /dev/null +++ b/assets/pages/Index.spec.js @@ -0,0 +1,14 @@ +import { shallowMount } from "@vue/test-utils"; +import Index from "./Index"; + +describe("", () => { + test("is a Vue instance", () => { + const wrapper = shallowMount(Index); + expect(wrapper.isVueInstance()).toBeTruthy(); + }); + + test("renders correctly", () => { + const wrapper = shallowMount(Index); + expect(wrapper.element).toMatchSnapshot(); + }); +}); diff --git a/assets/pages/__snapshots__/Container.spec.js.snap b/assets/pages/__snapshots__/Container.spec.js.snap new file mode 100644 index 00000000..f2fe4428 --- /dev/null +++ b/assets/pages/__snapshots__/Container.spec.js.snap @@ -0,0 +1,50 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` renders correctly 1`] = ` +
    + + +
      + + +
    +`; diff --git a/assets/pages/__snapshots__/Index.spec.js.snap b/assets/pages/__snapshots__/Index.spec.js.snap new file mode 100644 index 00000000..6b9b0689 --- /dev/null +++ b/assets/pages/__snapshots__/Index.spec.js.snap @@ -0,0 +1,21 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` renders correctly 1`] = ` +
    +
    +
    +

    + Please choose a container from the list to view the logs +

    +
    +
    +
    +`; diff --git a/package.json b/package.json index c8747907..4900fbe5 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,18 @@ "json", "vue" ], + "coveragePathIgnorePatterns": [ + "node_modules" + ], + "testPathIgnorePatterns": [ + "node_modules" + ], + "transformIgnorePatterns": [ + "node_modules" + ], + "watchPathIgnorePatterns": [ + "/node_modules/" + ], "snapshotSerializers": [ "jest-serializer-vue" ],