-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathHeuristicQuestionAnswer.spec.js
More file actions
143 lines (114 loc) · 4.63 KB
/
HeuristicQuestionAnswer.spec.js
File metadata and controls
143 lines (114 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import HeuristicQuestionAnswer from '@/ux/Heuristic/models/HeuristicQuestionAnswer'
describe('HeuristicQuestionAnswer', () => {
describe('constructor', () => {
it('sets all fields from provided data', () => {
const data = {
heuristicId: 1,
heuristicAnswer: { text: 'Good', value: 4 },
heuristicComment: 'Looks fine',
answerImageUrl: 'https://example.com/img.png',
}
const answer = new HeuristicQuestionAnswer(data)
expect(answer.heuristicId).toBe(1)
expect(answer.heuristicAnswer).toEqual({ text: 'Good', value: 4 })
expect(answer.heuristicComment).toBe('Looks fine')
expect(answer.answerImageUrl).toBe('https://example.com/img.png')
})
it('defaults heuristicAnswer to empty object when undefined', () => {
const answer = new HeuristicQuestionAnswer({})
expect(answer.heuristicAnswer).toEqual({})
})
it('handles no arguments (empty constructor)', () => {
const answer = new HeuristicQuestionAnswer()
expect(answer.heuristicId).toBeUndefined()
expect(answer.heuristicAnswer).toEqual({})
expect(answer.heuristicComment).toBeUndefined()
expect(answer.answerImageUrl).toBeUndefined()
})
})
describe('toFirestore', () => {
it('returns correct Firestore shape', () => {
const answer = new HeuristicQuestionAnswer({
heuristicId: 5,
heuristicAnswer: { text: 'Bad', value: 1 },
heuristicComment: 'Poor contrast',
answerImageUrl: 'https://example.com/screenshot.png',
})
expect(answer.toFirestore()).toEqual({
heuristicId: 5,
heuristicAnswer: { text: 'Bad', value: 1 },
heuristicComment: 'Poor contrast',
answerImageUrl: 'https://example.com/screenshot.png',
})
})
it('defaults answerImageUrl to empty string when falsy', () => {
const answer = new HeuristicQuestionAnswer({ heuristicId: 1 })
const result = answer.toFirestore()
expect(result.answerImageUrl).toBe('')
})
it('preserves answerImageUrl when provided', () => {
const answer = new HeuristicQuestionAnswer({
answerImageUrl: 'https://img.example.com/a.png',
})
expect(answer.toFirestore().answerImageUrl).toBe('https://img.example.com/a.png')
})
})
describe('toHeuristicQuestionAnswer (static factory)', () => {
const testOptions = [
{ text: 'Very Bad', value: 0 },
{ text: 'Bad', value: 1 },
{ text: 'Neutral', value: 2 },
{ text: 'Good', value: 3 },
{ text: 'Very Good', value: 4 },
]
it('keeps heuristicAnswer as-is when it already has a text property', () => {
const data = {
heuristicId: 10,
heuristicAnswer: { text: 'Custom', value: 99 },
heuristicComment: 'Already formatted',
}
const result = HeuristicQuestionAnswer.toHeuristicQuestionAnswer(data, testOptions)
expect(result).toBeInstanceOf(HeuristicQuestionAnswer)
expect(result.heuristicAnswer).toEqual({ text: 'Custom', value: 99 })
})
it('converts a numeric heuristicAnswer to object using testOptions', () => {
const data = {
heuristicId: 10,
heuristicAnswer: 3,
heuristicComment: 'Nice',
}
const result = HeuristicQuestionAnswer.toHeuristicQuestionAnswer(data, testOptions)
expect(result).toBeInstanceOf(HeuristicQuestionAnswer)
expect(result.heuristicAnswer).toEqual({ text: 'Good', value: 3 })
})
it('sets text to empty string when numeric value is not found in testOptions', () => {
const data = {
heuristicId: 10,
heuristicAnswer: 999,
}
const result = HeuristicQuestionAnswer.toHeuristicQuestionAnswer(data, testOptions)
expect(result.heuristicAnswer).toEqual({ text: '', value: 999 })
})
it('spreads remaining data fields onto the instance', () => {
const data = {
heuristicId: 7,
heuristicAnswer: 0,
heuristicComment: 'Terrible',
answerImageUrl: 'https://img.test/x.png',
}
const result = HeuristicQuestionAnswer.toHeuristicQuestionAnswer(data, testOptions)
expect(result.heuristicId).toBe(7)
expect(result.heuristicComment).toBe('Terrible')
expect(result.answerImageUrl).toBe('https://img.test/x.png')
expect(result.heuristicAnswer).toEqual({ text: 'Very Bad', value: 0 })
})
it('handles null heuristicAnswer gracefully', () => {
const data = {
heuristicId: 1,
heuristicAnswer: null,
}
const result = HeuristicQuestionAnswer.toHeuristicQuestionAnswer(data, testOptions)
expect(result.heuristicAnswer).toEqual({ text: '', value: null })
})
})
})