@@ -10,8 +10,7 @@ jest.mock('../src/connector', () =>
1010 } ) ) ,
1111) ;
1212
13- // TODO: Update the data source name.
14- const DATA_SOURCE_NAME = 'YourDataSource' ;
13+ const DATA_SOURCE_NAME = 'IMDBAPI' ;
1514
1615const connector = new Connector ( ) ;
1716const model = new Model ( { connector } ) ;
@@ -21,27 +20,134 @@ describe(`${DATA_SOURCE_NAME}Model`, () => {
2120 expect ( model ) . toBeInstanceOf ( GraphQLModel ) ;
2221 } ) ;
2322
24- // TODO: Update this test to use your model’s method(s).
25- describe ( 'getById()' , ( ) => {
23+ describe ( 'searchMoviesByTitle()' , ( ) => {
2624 it ( 'calls the correct endpoint with a given ID' , ( ) => {
2725 const spy = jest . spyOn ( connector , 'get' ) ;
2826
29- model . getById ( '1234' ) ;
30- expect ( spy ) . toHaveBeenCalledWith ( '/data/1234' ) ;
27+ model . searchMoviesByTitle ( { title : 'Test Movie' } ) ;
28+ expect ( spy ) . toHaveBeenCalledWith ( '/find/movie?title=Test+Movie' ) ;
29+ } ) ;
30+
31+ it ( 'correctly adds the year if one is supplied' , ( ) => {
32+ const spy = jest . spyOn ( connector , 'get' ) ;
33+
34+ model . searchMoviesByTitle ( { title : 'Test Movie' , year : '1979' } ) ;
35+ expect ( spy ) . toHaveBeenCalledWith (
36+ '/find/movie?title=Test+Movie&year=1979' ,
37+ ) ;
38+ } ) ;
39+
40+ it ( 'ignores the year if an empty value is provided' , ( ) => {
41+ const spy = jest . spyOn ( connector , 'get' ) ;
42+
43+ model . searchMoviesByTitle ( { title : 'Test Movie' , year : '' } ) ;
44+ expect ( spy ) . toHaveBeenCalledWith ( '/find/movie?title=Test+Movie' ) ;
45+ } ) ;
46+
47+ it ( 'throws a GrampsError if something goes wrong' , async ( ) => {
48+ expect . assertions ( 3 ) ;
49+
50+ model . connector . get . mockImplementationOnce ( ( ) =>
51+ Promise . reject ( Error ( 'boom' ) ) ,
52+ ) ;
53+
54+ try {
55+ await model . searchMoviesByTitle ( { title : 'Test Movie' } ) ;
56+ } catch ( error ) {
57+ expect ( error . isBoom ) . toEqual ( true ) ;
58+ expect ( error . output . payload . description ) . toEqual (
59+ 'Unable to search movies' ,
60+ ) ;
61+ expect ( error . output . payload . docsLink ) . toEqual (
62+ 'https://github.com/gramps-express/data-source-imdbapi' ,
63+ ) ;
64+ }
65+ } ) ;
66+ } ) ;
67+
68+ describe ( 'searchPersonByName()' , ( ) => {
69+ it ( 'calls the correct endpoint with a given ID' , ( ) => {
70+ const spy = jest . spyOn ( connector , 'get' ) ;
71+
72+ model . searchPersonByName ( 'Famous Person' ) ;
73+ expect ( spy ) . toHaveBeenCalledWith ( '/find/person?name=Famous+Person' ) ;
74+ } ) ;
75+
76+ it ( 'throws a GrampsError if something goes wrong' , async ( ) => {
77+ expect . assertions ( 3 ) ;
78+
79+ model . connector . get . mockImplementationOnce ( ( ) =>
80+ Promise . reject ( Error ( 'boom' ) ) ,
81+ ) ;
82+
83+ try {
84+ await model . searchPersonByName ( 'Famous Person' ) ;
85+ } catch ( error ) {
86+ expect ( error . isBoom ) . toEqual ( true ) ;
87+ expect ( error . output . payload . description ) . toEqual (
88+ 'Unable to search people' ,
89+ ) ;
90+ expect ( error . output . payload . docsLink ) . toEqual (
91+ 'https://github.com/gramps-express/data-source-imdbapi' ,
92+ ) ;
93+ }
94+ } ) ;
95+ } ) ;
96+
97+ describe ( 'getMovieById()' , ( ) => {
98+ it ( 'calls the correct endpoint with a given ID' , ( ) => {
99+ const spy = jest . spyOn ( connector , 'get' ) ;
100+
101+ model . getMovieById ( 'tt1234567' ) ;
102+ expect ( spy ) . toHaveBeenCalledWith ( '/movie?movie_id=tt1234567' ) ;
103+ } ) ;
104+
105+ it ( 'throws a GrampsError if something goes wrong' , async ( ) => {
106+ expect . assertions ( 3 ) ;
107+
108+ model . connector . get . mockImplementationOnce ( ( ) =>
109+ Promise . reject ( Error ( 'boom' ) ) ,
110+ ) ;
111+
112+ try {
113+ await model . getMovieById ( 'tt1234567' ) ;
114+ } catch ( error ) {
115+ expect ( error . isBoom ) . toEqual ( true ) ;
116+ expect ( error . output . payload . description ) . toEqual (
117+ 'Unable to get movie by ID' ,
118+ ) ;
119+ expect ( error . output . payload . docsLink ) . toEqual (
120+ 'https://github.com/gramps-express/data-source-imdbapi' ,
121+ ) ;
122+ }
123+ } ) ;
124+ } ) ;
125+
126+ describe ( 'getPersonById()' , ( ) => {
127+ it ( 'calls the correct endpoint with a given ID' , ( ) => {
128+ const spy = jest . spyOn ( connector , 'get' ) ;
129+
130+ model . getPersonById ( 'nm1234567' ) ;
131+ expect ( spy ) . toHaveBeenCalledWith ( '/person?person_id=nm1234567' ) ;
31132 } ) ;
32133
33134 it ( 'throws a GrampsError if something goes wrong' , async ( ) => {
34- expect . assertions ( 1 ) ;
135+ expect . assertions ( 3 ) ;
35136
36137 model . connector . get . mockImplementationOnce ( ( ) =>
37- Promise . reject ( { no : 'good' } ) ,
138+ Promise . reject ( Error ( 'boom' ) ) ,
38139 ) ;
39140
40141 try {
41- // TODO: Update to use one of your model’s methods.
42- await model . getById ( '1234' ) ;
142+ await model . getPersonById ( 'nm1234567' ) ;
43143 } catch ( error ) {
44144 expect ( error . isBoom ) . toEqual ( true ) ;
145+ expect ( error . output . payload . description ) . toEqual (
146+ 'Unable to get person by ID' ,
147+ ) ;
148+ expect ( error . output . payload . docsLink ) . toEqual (
149+ 'https://github.com/gramps-express/data-source-imdbapi' ,
150+ ) ;
45151 }
46152 } ) ;
47153 } ) ;
@@ -67,8 +173,7 @@ describe(`${DATA_SOURCE_NAME}Model`, () => {
67173 ) ;
68174
69175 try {
70- // TODO: Update to use one of your model’s methods.
71- await model . getById ( 1234 ) ;
176+ await model . searchMoviesByTitle ( { title : 'Test Movie' } ) ;
72177 } catch ( error ) {
73178 // Check that GrampsError properly received the error detail.
74179 expect ( error ) . toHaveProperty ( 'isBoom' , true ) ;
0 commit comments