diff --git a/src/tests/addQuestion.test.ts b/src/tests/addQuestion.test.ts new file mode 100644 index 0000000..85c3316 --- /dev/null +++ b/src/tests/addQuestion.test.ts @@ -0,0 +1,48 @@ +import { addQuestion, createQuestion } from '../utils/classes' +import { Subject, Question } from '../types/basicTypes' + +const question: Question = createQuestion('asd', 'asd', { type: 'simple' }) + +test('Adds questions to empty db', () => { + const emptyDb: Subject[] = [] + addQuestion(emptyDb, 'test subject', question) + expect(emptyDb.length).toBe(1) +}) + +test('Adds questions next to existing', () => { + const db: Subject[] = [ + { + Name: 'test subject', + Questions: [question], + }, + ] + addQuestion(db, 'another something', question) + expect(db.length).toBe(2) +}) + +test('Does not add new subject, multiple new questions', () => { + const db: Subject[] = [ + { + Name: 'test subject', + Questions: [question], + }, + ] + addQuestion(db, 'test subject', question) + addQuestion(db, 'test subject', question) + addQuestion(db, 'test subject', question) + addQuestion(db, 'test subject', question) + expect(db.length).toBe(1) +}) + +test('Adds new subjects, multiple new questions', () => { + const db: Subject[] = [ + { + Name: 'test subject', + Questions: [question], + }, + ] + addQuestion(db, 'gfjdkglfd', question) + addQuestion(db, ' somrthing test ', question) + addQuestion(db, 'aaaaaaaa', question) + expect(db.length).toBe(4) +}) diff --git a/src/utils/classes.ts b/src/utils/classes.ts index 5477a2e..d5eb488 100755 --- a/src/utils/classes.ts +++ b/src/utils/classes.ts @@ -460,10 +460,10 @@ function addQuestion( logger.DebugLog('Adding new question with subjName: ' + subj, 'qdb add', 1) logger.DebugLog(question, 'qdb add', 3) - const i = data.findIndex((x) => { - return !subj + const i = data.findIndex((subject) => { + return subj .toLowerCase() - .includes(getSubjNameWithoutYear(x.Name).toLowerCase()) + .includes(getSubjNameWithoutYear(subject.Name).toLowerCase()) }) if (i !== -1) {