Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix flaky form feedback test #2217

Merged
merged 2 commits into from
Jun 21, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/app/modules/feedback/__tests__/feedback.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ObjectId } from 'bson-ext'
import compareAsc from 'date-fns/compareAsc'
import { compareAsc } from 'date-fns'
import { times } from 'lodash'
import moment from 'moment-timezone'
import mongoose from 'mongoose'
Expand Down Expand Up @@ -139,19 +139,27 @@ describe('feedback.service', () => {
}))
// Act
const actualResult = await FeedbackService.getFormFeedbacks(mockFormId)
const actual = actualResult._unsafeUnwrap()

// Assert
// Should only average from the feedbacks for given formId.
const expectedAverage = (
expectedCreatedFbs.reduce((acc, curr) => acc + curr.rating, 0) /
expectedCount
).toFixed(2)
expect(actualResult.isOk()).toEqual(true)
expect(actualResult._unsafeUnwrap()).toEqual({
expect(actual).toEqual({
average: expectedAverage,
count: expectedCount,
feedback: expectedFeedbackList,
// Feedback may not be returned in same order, so perform unordered check
feedback: expect.arrayContaining(expectedFeedbackList),
})
// Check that there are no extra elements
expect(actual.feedback.length).toBe(expectedFeedbackList.length)
// Check that feedback is returned in date order. We cannot simply sort the arrays
// as the order is non-deterministic if the timestamps are identical.
expect(expectedFeedbackList.map((f) => f.timestamp)).toEqual(
actual.feedback.map((f) => f.timestamp),
)
})

it('should return feedback response with zero count and empty array when no feedback is available', async () => {
Expand Down