-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: modularize setup and simplify submission creation test
- Move test data initialization to to reduce redundancy. - Extract submission document creation to . - Simplify main test body to focus on core assertions.
- Loading branch information
Showing
2 changed files
with
57 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,63 @@ | ||
import { faker } from '@faker-js/faker'; | ||
import { db } from '../firebase/initFirebase'; | ||
import { doc, setDoc, serverTimestamp, getDoc } from 'firebase/firestore'; | ||
import { mintNFT } from '../functions/index'; | ||
import { faker } from '@faker-js/faker' | ||
import { db } from '../firebase/initFirebase' | ||
import { doc, setDoc, serverTimestamp, getDoc } from 'firebase/firestore' | ||
import { mintNFT } from '../functions/index' | ||
|
||
jest.mock('../functions/index', () => ({ | ||
mintNFT: jest.fn().mockResolvedValue(undefined), | ||
})); | ||
})) | ||
|
||
describe('mintNFT Function', () => { | ||
let lessonSubmission; | ||
let change; | ||
let context; | ||
let lessonSubmission, change, context | ||
|
||
beforeEach(() => { | ||
const randomContent = faker.string.alpha(10); | ||
setupTestData() | ||
}) | ||
|
||
const setupTestData = () => { | ||
lessonSubmission = { | ||
cohort_id: 'RU5mLpQrZZWlmftNSB2w', | ||
content: { | ||
type: 'text', | ||
value: randomContent, | ||
value: faker.string.alpha(10), | ||
}, | ||
createdAt: serverTimestamp(), | ||
lesson: 'Lesson_2_Add_State.md', | ||
section: 'Section_1', | ||
user: 'users/23WEoArqzRf4ORh4cdvadaVsYtj1', | ||
user_id: '23WEoArqzRf4ORh4cdvadaVsYtj1', | ||
}; | ||
change = { data: () => lessonSubmission }; | ||
context = { params: { lessonId: 'test-lesson-id' } }; | ||
}); | ||
|
||
it('should create a submission document in lessons_submissions', async () => { | ||
const submissionId = faker.string.uuid(); | ||
const docRef = doc(db, 'lessons_submissions', submissionId); | ||
} | ||
change = { data: () => lessonSubmission } | ||
context = { params: { lessonId: 'test-lesson-id' } } | ||
} | ||
|
||
const createSubmissionDocument = async (submissionId) => { | ||
const docRef = doc(db, 'lessons_submissions', submissionId) | ||
await setDoc(docRef, { | ||
...lessonSubmission, | ||
cohort: doc(db, `cohorts/${lessonSubmission.cohort_id}`), | ||
user: doc(db, lessonSubmission.user), | ||
}); | ||
}) | ||
return docRef | ||
} | ||
|
||
const submissionDoc = await getDoc(docRef); | ||
expect(submissionDoc.exists()).toBe(true); | ||
const data = submissionDoc.data(); | ||
it('should create a submission document in lessons_submissions', async () => { | ||
const submissionId = faker.string.uuid() | ||
const docRef = await createSubmissionDocument(submissionId) | ||
|
||
// Verify the created document's structure | ||
expect(data).toMatchObject({ | ||
const submissionDoc = await getDoc(docRef) | ||
expect(submissionDoc.exists()).toBe(true) | ||
expect(submissionDoc.data()).toMatchObject({ | ||
cohort_id: lessonSubmission.cohort_id, | ||
content: lessonSubmission.content, | ||
createdAt: expect.any(Object), | ||
lesson: lessonSubmission.lesson, | ||
section: lessonSubmission.section, | ||
user_id: lessonSubmission.user_id, | ||
}); | ||
}) | ||
|
||
// Check if Firestore trigger is called | ||
await mintNFT(change, context); | ||
expect(mintNFT).toHaveBeenCalledWith(change, context); | ||
}); | ||
}); | ||
// Check if Firestore trigger (mintNFT) is called | ||
await mintNFT(change, context) | ||
expect(mintNFT).toHaveBeenCalledWith(change, context) | ||
}) | ||
}) |