Unit tests for TaskPanel
This commit is contained in:
parent
9f72e5393b
commit
53c697312c
5 changed files with 111 additions and 6 deletions
|
@ -1,9 +1,12 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
transform: {
|
transform: {
|
||||||
'^.+\\.ts?$': 'ts-jest',
|
'^.+\\.ts?$': 'ts-jest',
|
||||||
'^.+\\.svelte$': 'svelte-jester',
|
'^.+\\.svelte$': [
|
||||||
|
'svelte-jester',
|
||||||
|
{ "preprocess": true }
|
||||||
|
]
|
||||||
},
|
},
|
||||||
testEnvironment: 'jsdom',
|
testEnvironment: 'jsdom',
|
||||||
testRegex: '.*\\.test?\\.ts$',
|
testRegex: '.*\\.test?\\.[tj]s$',
|
||||||
moduleFileExtensions: ['ts', 'js', 'svelte']
|
moduleFileExtensions: ['ts', 'js', 'svelte']
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,9 +12,9 @@ async function getToken(): Promise<string | undefined> {
|
||||||
return apitoken = token;
|
return apitoken = token;
|
||||||
}
|
}
|
||||||
const form = doc.getElementById("apitoken") as HTMLFormElement
|
const form = doc.getElementById("apitoken") as HTMLFormElement
|
||||||
const op = form.elements.namedItem("op")!.value
|
const op = (form.elements.namedItem("op") as HTMLInputElement).value
|
||||||
const submit = form.elements.namedItem("submit")!.value
|
const submit = (form.elements.namedItem("submit") as HTMLInputElement).value
|
||||||
const csrfToken = form.elements.namedItem("_token")!.value
|
const csrfToken = (form.elements.namedItem("_token") as HTMLInputElement).value
|
||||||
const body = `op=${encodeURIComponent(op)}&submit=${encodeURIComponent(submit)}&_token=${encodeURIComponent(csrfToken)}`
|
const body = `op=${encodeURIComponent(op)}&submit=${encodeURIComponent(submit)}&_token=${encodeURIComponent(csrfToken)}`
|
||||||
console.log(`Creating new API token`)
|
console.log(`Creating new API token`)
|
||||||
await fetch("/auth/apitoken.cgi", { method: "POST", body, headers: [["Content-Type", "application/x-www-form-urlencoded"]], redirect: "manual" })
|
await fetch("/auth/apitoken.cgi", { method: "POST", body, headers: [["Content-Type", "application/x-www-form-urlencoded"]], redirect: "manual" })
|
||||||
|
|
|
@ -2,7 +2,6 @@ import * as g from '../ksp-task-grabber'
|
||||||
import { readFileSync, readdirSync } from 'fs';
|
import { readFileSync, readdirSync } from 'fs';
|
||||||
import { resolve } from 'path';
|
import { resolve } from 'path';
|
||||||
import type { TaskDescriptor, TasksFile } from '../tasks';
|
import type { TaskDescriptor, TasksFile } from '../tasks';
|
||||||
import { action_destroyer } from 'svelte/internal';
|
|
||||||
|
|
||||||
const node_fetch: any = require('node-fetch')
|
const node_fetch: any = require('node-fetch')
|
||||||
|
|
||||||
|
|
96
frontend/src/tests/ui-jsdom.test.js
Normal file
96
frontend/src/tests/ui-jsdom.test.js
Normal file
|
@ -0,0 +1,96 @@
|
||||||
|
const { readFileSync, readdirSync } = require('fs');
|
||||||
|
const { resolve } = require('path');
|
||||||
|
const TaskPanel = require('../TaskPanel.svelte').default
|
||||||
|
|
||||||
|
|
||||||
|
const node_fetch = require('node-fetch')
|
||||||
|
|
||||||
|
const delay = (time=5) => new Promise((resolve) => window.setTimeout(resolve, time))
|
||||||
|
const allPromises = []
|
||||||
|
|
||||||
|
global.fetch = function(url, init) {
|
||||||
|
const p = node_fetch(new URL(url, "https://ksp.mff.cuni.cz").href, init)
|
||||||
|
allPromises.push(p)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
const tasks_json = readFileSync("../tasks.json").toString()
|
||||||
|
const tasks = JSON.parse(tasks_json)
|
||||||
|
|
||||||
|
test("TaskPanel - display", async () => {
|
||||||
|
allPromises.length = 0
|
||||||
|
|
||||||
|
const element = document.createElement("div")
|
||||||
|
const tp = new TaskPanel({
|
||||||
|
target: element,
|
||||||
|
props: { tasks, selectedTaskId: "26-Z1-1" }
|
||||||
|
})
|
||||||
|
await Promise.all(allPromises)
|
||||||
|
await delay()
|
||||||
|
expect(element.querySelector("h3").textContent).toEqual("Kevin a magnety")
|
||||||
|
expect(element.querySelector(".leftfloat pre").textContent.trim()).toEqual("4\n+-\n+-\n-+\n+-")
|
||||||
|
expect(element.querySelector(".status").textContent.trim()).toEqual("26-Z1-1 | 8 bodů")
|
||||||
|
expect(element.textContent).toContain("Pro odevzdávání je potřeba se přihlásit.")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("TaskPanel - solution", async () => {
|
||||||
|
allPromises.length = 0
|
||||||
|
|
||||||
|
const element = document.createElement("div")
|
||||||
|
const tp = new TaskPanel({
|
||||||
|
target: element,
|
||||||
|
props: { tasks, selectedTaskId: "26-Z1-1" }
|
||||||
|
})
|
||||||
|
await Promise.all(allPromises)
|
||||||
|
await delay()
|
||||||
|
|
||||||
|
const showSolution = element.querySelector(".solution a")
|
||||||
|
expect(showSolution).toBeTruthy()
|
||||||
|
showSolution.click()
|
||||||
|
await delay()
|
||||||
|
|
||||||
|
const sol = element.querySelector(".solution")
|
||||||
|
expect(sol.textContent).toContain("opravdu si chceš vyzradit řešení?")
|
||||||
|
sol.querySelector("p").remove()
|
||||||
|
const result = eval(sol.textContent.replace("=", ""))
|
||||||
|
const input = sol.querySelector("input")
|
||||||
|
|
||||||
|
input.value = result + 1
|
||||||
|
input.dispatchEvent(new Event('input', {
|
||||||
|
bubbles: true,
|
||||||
|
cancelable: true
|
||||||
|
}))
|
||||||
|
await delay()
|
||||||
|
|
||||||
|
expect(element.querySelector(".solution input")).toBe(input)
|
||||||
|
expect(element.querySelector(".solution").textContent).not.toContain("Načítám")
|
||||||
|
|
||||||
|
|
||||||
|
input.value = result
|
||||||
|
input.dispatchEvent(new Event('input', {
|
||||||
|
bubbles: true,
|
||||||
|
cancelable: true
|
||||||
|
}))
|
||||||
|
await delay()
|
||||||
|
expect(element.querySelector(".solution input")).toBeFalsy()
|
||||||
|
expect(element.querySelector(".solution").textContent).toContain("Načítám")
|
||||||
|
await Promise.all(allPromises)
|
||||||
|
await delay()
|
||||||
|
|
||||||
|
expect(sol.textContent).toContain("Magnety se přitahují tehdy, když mají naproti sobě opačné póly,")
|
||||||
|
expect(sol.textContent).toContain("Program (C)")
|
||||||
|
|
||||||
|
tp.$set({ selectedTaskId: "32-Z2-2" })
|
||||||
|
await delay()
|
||||||
|
await Promise.all(allPromises)
|
||||||
|
await delay()
|
||||||
|
// check that solution is not displayed
|
||||||
|
expect(element.querySelector(".solution").textContent.trim()).toEqual("Zobrazit řešení úlohy")
|
||||||
|
|
||||||
|
tp.$set({ selectedTaskId: "kucharka-zakladni-pole" })
|
||||||
|
await delay()
|
||||||
|
await Promise.all(allPromises)
|
||||||
|
await delay()
|
||||||
|
// solution does not make sense
|
||||||
|
expect(element.querySelector(".solution")).toBeNull()
|
||||||
|
})
|
7
frontend/svelte.config.js
Normal file
7
frontend/svelte.config.js
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
const sveltePreprocess = require("svelte-preprocess");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
preprocess: sveltePreprocess({
|
||||||
|
|
||||||
|
}),
|
||||||
|
};
|
Reference in a new issue