@@ -10,32 +10,20 @@ import dummyAlertData from '../../../../../../__dummy__/alertData'
1010import getExercisesData from '../../../../../../__dummy__/getExercisesData'
1111import '@testing-library/jest-dom'
1212import GET_APP from '../../../../../../graphql/queries/getApp'
13+ import { MODULES } from '../../../../../../graphql/queries/modules'
14+ import { CHALLENGES } from '../../../../../../graphql/queries/challenges'
15+ import ADD_CHALLENGE from '../../../../../../graphql/queries/createChallenge'
1316import GET_EXERCISES from '../../../../../../graphql/queries/getExercises'
1417import REMOVE_EXERCISE_FLAG from '../../../../../../graphql/queries/removeExerciseFlag'
1518import DELETE_EXERCISE from '../../../../../../graphql/queries/deleteExercise'
1619import { MockedProvider } from '@apollo/client/testing'
17- import { gql } from '@apollo/client'
1820import userEvent from '@testing-library/user-event'
1921import UPDATE_LESSON from '../../../../../../graphql/queries/updateLesson'
2022
2123// Imported to be able to use expect(...).toBeInTheDocument()
2224import '@testing-library/jest-dom'
2325import { AddModuleDocument } from '../../../../../../graphql'
2426
25- const MODULES = gql `
26- query {
27- modules {
28- id
29- name
30- content
31- lesson {
32- id
33- }
34- order
35- }
36- }
37- `
38-
3927const modules = [
4028 {
4129 id : 1 ,
@@ -236,13 +224,44 @@ const unflagExerciseMock = {
236224 }
237225}
238226
227+ const challenges = dummyLessonData . find ( e => e . slug === 'js1' ) . challenges
228+ const challengesQueryMock = {
229+ request : { query : CHALLENGES , variables : { } } ,
230+ result : {
231+ data : {
232+ challenges
233+ }
234+ }
235+ }
236+
237+ const addChallengeQueryMock = {
238+ request : {
239+ query : ADD_CHALLENGE ,
240+ variables : {
241+ content : 'Write an app that delete assets' ,
242+ lessonId : 2 ,
243+ name : 'Asset deletion' ,
244+ order : 1 ,
245+ description : 'Write an app that delete assets' ,
246+ title : 'Asset deletion'
247+ }
248+ } ,
249+ result : {
250+ data : {
251+ createChallenge : dummyLessonData [ 0 ]
252+ }
253+ }
254+ }
255+
239256const mocks = [
240257 getAppQueryMock ,
241258 getAppQueryMock ,
242259 getAppQueryMock ,
243260 modulesQueryMock ,
244261 updateLessonMutationMock ,
245262 addModuleMock ,
263+ challengesQueryMock ,
264+ addChallengeQueryMock ,
246265 getExercisesMock
247266]
248267
@@ -251,6 +270,8 @@ const mocksWithError = [
251270 modulesQueryMock ,
252271 updateLessonMutationMockWithError ,
253272 addModuleMock ,
273+ challengesQueryMock ,
274+ addChallengeQueryMock ,
254275 getExercisesMock
255276]
256277
@@ -260,7 +281,9 @@ const mocksWithRefetchExercises = [
260281 getAppQueryMock ,
261282 getExerciseWithRefetchMock ,
262283 deleteExerciseMock ,
263- unflagExerciseMock
284+ unflagExerciseMock ,
285+ challengesQueryMock ,
286+ addChallengeQueryMock
264287]
265288
266289const useRouter = jest . spyOn ( require ( 'next/router' ) , 'useRouter' )
@@ -460,6 +483,119 @@ describe('modules', () => {
460483 } )
461484} )
462485
486+ describe ( 'challenges' , ( ) => {
487+ const useRouterChallenges = {
488+ ...useRouterObj ,
489+ asPath : 'c0d3.com/admin/lessons/js1/challenges' ,
490+ query : {
491+ ...useRouterObj . query ,
492+ pageName : 'challenges'
493+ }
494+ }
495+
496+ beforeAll ( ( ) => useRouter . mockImplementation ( ( ) => useRouterChallenges ) )
497+
498+ it ( 'Should render challenges page' , async ( ) => {
499+ expect . assertions ( 1 )
500+
501+ render (
502+ < MockedProvider mocks = { mocks } >
503+ < LessonPage />
504+ </ MockedProvider >
505+ )
506+
507+ // Used to make the queries resolve
508+ await act ( ( ) => new Promise ( res => setTimeout ( res , 0 ) ) )
509+
510+ expect ( screen . getByText ( 'Incremental Closure' ) ) . toBeInTheDocument ( )
511+ } )
512+
513+ it ( 'Should add a challenge' , async ( ) => {
514+ expect . assertions ( 1 )
515+
516+ render (
517+ < MockedProvider mocks = { mocks } >
518+ < LessonPage />
519+ </ MockedProvider >
520+ )
521+
522+ // Used to make the queries resolve
523+ await act ( ( ) => new Promise ( res => setTimeout ( res , 0 ) ) )
524+
525+ // Will be changed to ADD NEW CHALLENGE in a subsequent PR
526+ await userEvent . click ( screen . getByText ( 'ADD NEW MODULE' ) )
527+
528+ // Challenge name
529+ await userEvent . type ( screen . getByTestId ( 'input0' ) , 'Asset deletion' , {
530+ delay : 1
531+ } )
532+
533+ // Challenge description
534+ await userEvent . type (
535+ screen . getByTestId ( 'textbox' ) ,
536+ 'Write an app that delete assets' ,
537+ {
538+ delay : 1
539+ }
540+ )
541+
542+ // Challenge order
543+ await userEvent . type ( screen . getByTestId ( 'input2' ) , '1' , {
544+ delay : 1
545+ } )
546+
547+ const submit = screen . getByText ( 'ADD CHALLENGE' )
548+
549+ await userEvent . click ( submit )
550+
551+ expect (
552+ await screen . findByText ( 'Added the item successfully!' )
553+ ) . toBeInTheDocument ( )
554+ } )
555+
556+ it ( 'Should set "add challenge" mode' , async ( ) => {
557+ expect . assertions ( 3 )
558+
559+ render (
560+ < MockedProvider mocks = { mocks } >
561+ < LessonPage />
562+ </ MockedProvider >
563+ )
564+
565+ // Used to make the queries resolve
566+ await act ( ( ) => new Promise ( res => setTimeout ( res , 0 ) ) )
567+
568+ // Will be changed to ADD NEW CHALLENGE in a subsequent PR
569+ await userEvent . click ( screen . getByText ( 'ADD NEW MODULE' ) )
570+
571+ expect ( screen . getByTestId ( 'input0' ) . value ) . toBe ( '' )
572+ expect ( screen . getByTestId ( 'input2' ) . value ) . toBe ( '' )
573+ expect ( screen . getByTestId ( 'textbox' ) . value ) . toBe ( '' )
574+ } )
575+
576+ it ( 'Should switch to another challenge' , async ( ) => {
577+ expect . assertions ( 2 )
578+
579+ render (
580+ < MockedProvider mocks = { mocks } >
581+ < LessonPage />
582+ </ MockedProvider >
583+ )
584+
585+ // Used to make the queries resolve
586+ await act ( ( ) => new Promise ( res => setTimeout ( res , 0 ) ) )
587+
588+ const selectedChallenge = challenges [ 0 ]
589+
590+ await userEvent . click ( screen . getByText ( selectedChallenge . title ) )
591+
592+ expect ( screen . getByTestId ( 'input0' ) . value ) . toBe ( selectedChallenge . title )
593+ expect ( screen . getByTestId ( 'textbox' ) . value ) . toBe (
594+ selectedChallenge . description
595+ )
596+ } )
597+ } )
598+
463599describe ( 'introduction' , ( ) => {
464600 const useRouterIntroduction = {
465601 ...useRouterObj ,
@@ -639,7 +775,7 @@ describe('exercises', () => {
639775 expect ( ( await screen . findAllByText ( 'Email' ) ) . length ) . toBe ( 3 )
640776 } )
641777
642- it ( 'Should not render exercises if they are none' , async ( ) => {
778+ it ( 'Should not render exercises if they are none flagged ' , async ( ) => {
643779 expect . assertions ( 1 )
644780
645781 const mocksOfMocks = mocks . slice ( 0 , - 1 )
0 commit comments