stricter subj name checking on question add, should log test fix

This commit is contained in:
mrfry 2023-03-07 19:43:55 +01:00
parent 537f4c413e
commit d962abfbc2
5 changed files with 144 additions and 51 deletions

View file

@ -1,5 +1,11 @@
import { addQuestion, createQuestion } from '../utils/classes'
import {
addQuestion,
createQuestion,
getSubjNameWithoutYear,
} from '../utils/classes'
import { Subject, Question } from '../types/basicTypes'
import fs from 'fs'
import { RecievedData } from '../utils/actions'
const question: Question = createQuestion('asd', 'asd', { type: 'simple' })
@ -46,3 +52,83 @@ test('Adds new subjects, multiple new questions', () => {
addQuestion(db, 'aaaaaaaa', question)
expect(db.length).toBe(4)
})
test("New subject names shouldn't be empty", () => {
const data: RecievedData = JSON.parse(
fs.readFileSync(
__dirname + '/../../devel/tests/testData/ROS.json',
'utf8'
)
)
const db: Subject[] = [
{
Name: 'test subject',
Questions: [question],
},
]
data.quiz.forEach((question) => {
const subjName = getSubjNameWithoutYear(data.subj)
addQuestion(db, subjName, question)
})
db.forEach((subj) => {
expect(subj.Name).toBeTruthy()
})
})
test('New questions shouldnt be added to accidental empty named subjects', () => {
const data: RecievedData = JSON.parse(
fs.readFileSync(
__dirname + '/../../devel/tests/testData/ROS.json',
'utf8'
)
)
const db: Subject[] = [
{
Name: '',
Questions: [question],
},
]
data.quiz.forEach((question) => {
const subjName = getSubjNameWithoutYear(data.subj)
addQuestion(db, subjName, question)
})
expect(db[0].Questions.length).toBe(1)
expect(db.length).toBe(2)
})
test('Question gets added to the correct subject', () => {
const data: RecievedData = JSON.parse(
fs.readFileSync(
__dirname + '/../../devel/tests/testData/ROS.json',
'utf8'
)
)
const subjName = getSubjNameWithoutYear(data.subj)
const db: Subject[] = [
{
Name: subjName,
Questions: [question],
},
{
Name: data.subj, // subj name with course date in it (2022/23/2 -)
Questions: [question],
},
]
data.quiz.forEach((question) => {
addQuestion(db, subjName, question)
})
data.quiz.forEach((question) => {
addQuestion(db, 'some other subject name', question)
})
expect(db[0].Questions.length).toBe(9)
expect(db[1].Questions.length).toBe(1)
expect(db[2].Questions.length).toBe(8)
expect(db.length).toBe(3)
})