@@ -3,7 +3,7 @@ import React from 'react';
3
3
import { Provider } from 'react-redux' ;
4
4
import { readEndpoint } from 'redux-json-api' ;
5
5
import { mount , shallow } from 'enzyme' ;
6
- import Query from '../Query' ;
6
+ import { Query } from '../Query' ;
7
7
import { mockStore } from './utils' ;
8
8
9
9
let mockReadEndpoint ;
@@ -15,12 +15,17 @@ jest.mock('redux-json-api', () => ({
15
15
16
16
beforeEach ( ( ) => {
17
17
props = {
18
+ cacheEnabled : false ,
18
19
children : ( ) => < div /> ,
19
20
dispatch : jest . fn ( io => io ) ,
20
21
endpoint : '/posts' ,
21
22
} ;
22
23
23
- mockReadEndpoint = Promise . resolve ( { data : [ ] } ) ;
24
+ mockReadEndpoint = Promise . resolve ( { body : { data : [ ] } } ) ;
25
+ } ) ;
26
+
27
+ afterEach ( ( ) => {
28
+ jest . clearAllMocks ( ) ;
24
29
} ) ;
25
30
26
31
it ( 'renders without throwing' , ( ) => {
@@ -41,9 +46,7 @@ it('calls readEndpoint with given path', () => {
41
46
it ( 'saves ids of returned resources to state' , async ( ) => {
42
47
mockReadEndpoint = Promise . resolve ( {
43
48
body : {
44
- data : [
45
- { type : 'users' , id : '1' , attributes : { name : 'Wonderwoman' } } ,
46
- ] ,
49
+ data : [ { type : 'users' , id : '1' , attributes : { name : 'Wonderwoman' } } ] ,
47
50
} ,
48
51
} ) ;
49
52
const wrapper = shallow ( < Query { ...props } /> ) ;
@@ -57,7 +60,9 @@ it('updates loading state upon fetch initialization', () => {
57
60
} ) ;
58
61
59
62
it ( 'updates loading state when fetch has succeeded' , async ( ) => {
60
- mockReadEndpoint = Promise . resolve ( { data : { type : 'users' , id : '1' } } ) ;
63
+ mockReadEndpoint = Promise . resolve ( {
64
+ body : { data : { type : 'users' , id : '1' } } ,
65
+ } ) ;
61
66
const wrapper = shallow ( < Query { ...props } /> ) ;
62
67
await mockReadEndpoint ;
63
68
expect ( wrapper . state ( 'loading' ) ) . toBe ( false ) ;
@@ -69,3 +74,13 @@ it('updates loading state when fetch fails', async () => {
69
74
await mockReadEndpoint ;
70
75
expect ( wrapper . state ( 'loading' ) ) . toBe ( false ) ;
71
76
} ) ;
77
+
78
+ it ( 'only makes once request for same endpoint when requested more times' , async ( ) => {
79
+ mockReadEndpoint = Promise . resolve ( {
80
+ body : { data : { type : 'users' , id : '1' } } ,
81
+ } ) ;
82
+ shallow ( < Query { ...props } cacheEnabled /> ) ;
83
+ await mockReadEndpoint ;
84
+ shallow ( < Query { ...props } cacheEnabled /> ) ;
85
+ expect ( readEndpoint ) . toHaveBeenCalledTimes ( 1 ) ;
86
+ } ) ;
0 commit comments