@@ -35,10 +35,104 @@ describe('actions', () => {
3535 } ,
3636 } ,
3737 syntaxes : [ ] ,
38+ pagination : { } ,
3839 } ) ;
3940 } ) ;
4041
41- it ( 'should create an action to fetch recent snippets' , async ( ) => {
42+ it ( 'should create an action to set pagination links' , ( ) => {
43+ const links = {
44+ first : {
45+ limit : '20' ,
46+ rel : 'first' ,
47+ url : 'http://api.xsnippet.org/snippets?limit=20' ,
48+ } ,
49+ next : {
50+ limit : '20' ,
51+ marker : 28 ,
52+ rel : 'next' ,
53+ url : 'http://api.xsnippet.org/snippets?limit=20&marker=28' ,
54+ } ,
55+ prev : {
56+ limit : '20' ,
57+ rel : 'prev' ,
58+ url : 'http://api.xsnippet.org/snippets?limit=20' ,
59+ } ,
60+ } ;
61+ const store = createStore ( ) ;
62+ store . dispatch ( actions . setPaginationLinks ( links ) ) ;
63+
64+ expect ( store . getState ( ) . toJS ( ) ) . toEqual ( {
65+ recent : [ ] ,
66+ snippets : { } ,
67+ syntaxes : [ ] ,
68+ pagination : links ,
69+ } ) ;
70+ } ) ;
71+
72+ it ( 'should create an action to fetch recent snippets with marker' , async ( ) => {
73+ const snippets = [
74+ {
75+ id : 1 ,
76+ content : 'test' ,
77+ syntax : 'JavaScript' ,
78+ } ,
79+ {
80+ id : 2 ,
81+ content : 'batman' ,
82+ syntax : 'Python' ,
83+ } ,
84+ ] ;
85+ const links = '<http://api.xsnippet.org/snippets?limit=20>; rel="first", <http://api.xsnippet.org/snippets?limit=20&marker=19>; rel="next", <http://api.xsnippet.org/snippets?limit=20&marker=59>; rel="prev"' ;
86+
87+ fetchMock . getOnce (
88+ 'http://api.xsnippet.org/snippets?limit=20&marker=39' ,
89+ {
90+ headers : { Link : links } ,
91+ body : snippets ,
92+ } ,
93+ ) ;
94+
95+ const store = createStore ( ) ;
96+ await store . dispatch ( actions . fetchRecentSnippets ( 39 ) ) ;
97+
98+ expect ( store . getState ( ) . toJS ( ) ) . toEqual ( {
99+ recent : [ 1 , 2 ] ,
100+ snippets : {
101+ 1 : {
102+ id : 1 ,
103+ content : 'test' ,
104+ syntax : 'JavaScript' ,
105+ } ,
106+ 2 : {
107+ id : 2 ,
108+ content : 'batman' ,
109+ syntax : 'Python' ,
110+ } ,
111+ } ,
112+ syntaxes : [ ] ,
113+ pagination : {
114+ first : {
115+ limit : '20' ,
116+ rel : 'first' ,
117+ url : 'http://api.xsnippet.org/snippets?limit=20' ,
118+ } ,
119+ next : {
120+ limit : '20' ,
121+ marker : '19' ,
122+ rel : 'next' ,
123+ url : 'http://api.xsnippet.org/snippets?limit=20&marker=19' ,
124+ } ,
125+ prev : {
126+ limit : '20' ,
127+ marker : '59' ,
128+ rel : 'prev' ,
129+ url : 'http://api.xsnippet.org/snippets?limit=20&marker=59' ,
130+ } ,
131+ } ,
132+ } ) ;
133+ } ) ;
134+
135+ it ( 'should create an action to fetch recent snippets without marker' , async ( ) => {
42136 const snippets = [
43137 {
44138 id : 1 ,
@@ -51,11 +145,18 @@ describe('actions', () => {
51145 syntax : 'Python' ,
52146 } ,
53147 ] ;
148+ const links = '<http://api.xsnippet.org/snippets?limit=20>; rel="first", <http://api.xsnippet.org/snippets?limit=20&marker=39>; rel="next"' ;
54149
55- fetchMock . getOnce ( 'http://api.xsnippet.org/snippets' , JSON . stringify ( snippets ) ) ;
150+ fetchMock . getOnce (
151+ 'http://api.xsnippet.org/snippets?limit=20' ,
152+ {
153+ headers : { Link : links } ,
154+ body : snippets ,
155+ } ,
156+ ) ;
56157
57158 const store = createStore ( ) ;
58- await store . dispatch ( actions . fetchRecentSnippets ) ;
159+ await store . dispatch ( actions . fetchRecentSnippets ( ) ) ;
59160
60161 expect ( store . getState ( ) . toJS ( ) ) . toEqual ( {
61162 recent : [ 1 , 2 ] ,
@@ -72,6 +173,19 @@ describe('actions', () => {
72173 } ,
73174 } ,
74175 syntaxes : [ ] ,
176+ pagination : {
177+ first : {
178+ limit : '20' ,
179+ rel : 'first' ,
180+ url : 'http://api.xsnippet.org/snippets?limit=20' ,
181+ } ,
182+ next : {
183+ limit : '20' ,
184+ marker : '39' ,
185+ rel : 'next' ,
186+ url : 'http://api.xsnippet.org/snippets?limit=20&marker=39' ,
187+ } ,
188+ } ,
75189 } ) ;
76190 } ) ;
77191
@@ -94,6 +208,7 @@ describe('actions', () => {
94208 } ,
95209 } ,
96210 syntaxes : [ ] ,
211+ pagination : { } ,
97212 } ) ;
98213 } ) ;
99214
@@ -119,6 +234,7 @@ describe('actions', () => {
119234 } ,
120235 } ,
121236 syntaxes : [ ] ,
237+ pagination : { } ,
122238 } ) ;
123239 } ) ;
124240
@@ -130,6 +246,7 @@ describe('actions', () => {
130246 expect ( store . getState ( ) . toJS ( ) ) . toEqual ( {
131247 recent : [ ] ,
132248 snippets : { } ,
249+ pagination : { } ,
133250 syntaxes,
134251 } ) ;
135252 } ) ;
@@ -145,6 +262,7 @@ describe('actions', () => {
145262 expect ( store . getState ( ) . toJS ( ) ) . toEqual ( {
146263 recent : [ ] ,
147264 snippets : { } ,
265+ pagination : { } ,
148266 syntaxes,
149267 } ) ;
150268 } ) ;
@@ -171,6 +289,7 @@ describe('actions', () => {
171289 } ,
172290 } ,
173291 syntaxes : [ ] ,
292+ pagination : { } ,
174293 } ) ;
175294 } ) ;
176295} ) ;
0 commit comments