Skip to content

Commit 695f2f1

Browse files
committed
updated jest config globals, updated App component unit test from code review
1 parent 4039a4d commit 695f2f1

File tree

2 files changed

+41
-56
lines changed

2 files changed

+41
-56
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ module.exports = {
1818
setupFiles: ["./lib/testSetup.js"],
1919
testURL: "http://localhost/",
2020
globals: {
21-
SIMULATING: process.env.SIMULATING === "true" ? true : false
21+
SIMULATING: process.env.SIMULATING === "true"
2222
}
2323
};

public/js/components/specs/app.spec.js renamed to public/js/components/specs/App.spec.js

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { mount, shallowMount } from "@vue/test-utils";
22
import App from "../App";
3+
import * as configFns from "../../lib/configuration";
34
import { TITLE_EMOJI_REGEX } from "../../utils/constants.js";
45

56
// Mock dashboard data
@@ -111,9 +112,19 @@ describe("Number card", () => {
111112
});
112113

113114
test("onSaveSettings method", () => {
114-
const { vm } = shallowMountApp();
115+
jest.spyOn(configFns, "saveDashboard");
116+
const wrapper = shallowMountApp();
117+
118+
wrapper.vm.dashboard.editMode = "unlocked";
119+
120+
wrapper.find({ name: "dashboard-settings" }).vm.$emit("save-settings", {
121+
bgColor: "#fff",
122+
bgImageRepeat: true,
123+
bgImageUrl: "",
124+
title: "\u2700 IoT Dashboard"
125+
});
115126

116-
expect(vm.dashboard).toEqual(
127+
expect(wrapper.vm.dashboard).toEqual(
117128
expect.objectContaining({
118129
bgColor: expect.any(String),
119130
bgImageRepeat: expect.any(Boolean),
@@ -124,30 +135,7 @@ describe("Number card", () => {
124135
title: expect.any(String)
125136
})
126137
);
127-
128-
vm.onSaveSettings(event => {
129-
event = {
130-
bgColor: "#fff",
131-
bgImageRepeat: true,
132-
bgImageUrl: "",
133-
title: "\u2700 IoT Dashboard"
134-
};
135-
expect(vm.saveDashboard).toHaveBeenCalled();
136-
});
137-
138-
Vue.nextTick(() => {
139-
expect(vm.dashboard).toEqual(
140-
expect.objectContaining({
141-
bgColor: expect.any(String),
142-
bgImageRepeat: expect.any(Boolean),
143-
bgImageUrl: expect.any(String),
144-
blockSize: expect.any(Array),
145-
editMode: expect.any(String),
146-
tiles: expect.any(Array),
147-
title: expect.any(String)
148-
})
149-
);
150-
});
138+
expect(configFns.saveDashboard).toHaveBeenCalled();
151139
});
152140

153141
test("onTileChange method", () => {
@@ -159,7 +147,6 @@ describe("Number card", () => {
159147

160148
vm.onTileChange(event => {
161149
event.id = mockDashboardData.dashboard.tiles[0].id;
162-
expect(vm.saveDashboard).toHaveBeenCalled();
163150
});
164151

165152
expect(vm.dashboard.tiles[0]).toEqual({
@@ -188,44 +175,42 @@ describe("Number card", () => {
188175
});
189176

190177
test("onTileCreate method", () => {
191-
const { vm } = shallowMountApp();
178+
jest.spyOn(configFns, "saveDashboard");
179+
const wrapper = shallowMountApp();
192180

193-
vm.onTileCreate(event => {
194-
event = {
195-
buttonText: "begin",
196-
deviceId: "DH7643",
197-
deviceMethod: "halt",
198-
id: "8071d5ab-0z12-41i3-ba9p-f600124feb6b",
199-
position: [106, 266],
200-
size: [0.9, 0.3],
201-
title: "MSchip receiving",
202-
type: "button"
203-
};
181+
wrapper.find({ name: "dashboard-settings" }).vm.$emit("tile-create", {
182+
deviceId: "",
183+
id: "2ece272b-a403-46d6-b136-e35906fe1d0d",
184+
lineColor: "#FF6384",
185+
position: [0, 0],
186+
property: "",
187+
size: [2, 1.5],
188+
title: "Line Chart",
189+
type: "line-chart"
204190
});
205191

206-
expect(vm.dashboard.tiles.length).toBe(3);
192+
expect(configFns.saveDashboard).toHaveBeenCalled();
193+
expect(wrapper.vm.dashboard.tiles.length).toBe(3);
207194
});
208195

209-
test("onDeviceListRecieved method", () => {
196+
test("onDeviceListReceived method", () => {
210197
const { vm } = shallowMountApp();
211-
const io = jest.fn();
212-
const socket = io();
198+
let deviceList;
213199

214-
vm.onDeviceListReceived(() => {
215-
expect(socket).toHaveBeenCalled();
216-
});
200+
vm.onDeviceListReceived(deviceList);
201+
202+
expect(deviceList).toEqual(vm.deviceList);
203+
204+
// TODO: test socket.on callback function
217205
});
218206

219-
test("the getDashboard and getDeviceList functions in the created lifecycle hook", () => {
220-
const { vm } = mount(App, {
221-
methods: {
222-
getDashboard: () => mockDashboardData.dashboard,
223-
getDeviceList: () => mockDeviceList
224-
}
225-
});
207+
test("the getDashboard and getDeviceList are invoked in the created lifecycle hook", () => {
208+
jest.spyOn(configFns, "getDashboard");
209+
jest.spyOn(configFns, "getDeviceList");
210+
mount(App);
226211

227-
expect(vm.getDashboard()).toEqual(mockDashboardData.dashboard);
228-
expect(vm.getDeviceList()).toEqual(mockDeviceList);
212+
expect(configFns.getDashboard).toHaveBeenCalled();
213+
expect(configFns.getDeviceList).toHaveBeenCalled();
229214
});
230215
});
231216

0 commit comments

Comments
 (0)