1+ import { render , wait , cleanup } from '@testing-library/react'
2+ import { wrap , configureMocks , getNotUtilizedResponses } from '../src/index'
3+ import { MyComponentMakingHttpCalls , MyComponentRepeatingHttpCalls } from './components.mock'
4+ import { refreshProductsList } from './helpers'
5+
6+ configureMocks ( { defaultHost : 'my-host' , mount : render } )
7+
8+ afterEach ( ( ) => {
9+ cleanup ( )
10+ jest . restoreAllMocks ( )
11+ } )
12+
13+ it ( 'should warn when there are responses not being used' , async ( ) => {
14+ const consoleWarn = jest . spyOn ( console , 'warn' ) . mockImplementation ( )
15+
16+ wrap ( MyComponentMakingHttpCalls )
17+ . withMocks ( [
18+ { path : '/path/to/get/quantity/' , responseBody : '15' } ,
19+ { path : '/path/to/endpoint/not/being/used/' , responseBody : 'I am not being used' } ,
20+ ] )
21+ . mount ( )
22+
23+ await wait ( ( ) => {
24+ getNotUtilizedResponses ( )
25+ expect ( consoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'the following responses are not being used:' ) )
26+ expect ( consoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( '/path/to/endpoint/not/being/used/' ) )
27+ expect ( consoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'get' ) )
28+ expect ( consoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'I am not being used' ) )
29+ } )
30+ } )
31+
32+ it ( 'should warn when there are multiple responses not being used' , async ( ) => {
33+ const consoleWarn = jest . spyOn ( console , 'warn' ) . mockImplementation ( )
34+
35+ wrap ( MyComponentMakingHttpCalls )
36+ . withMocks ( [
37+ { path : '/path/to/get/quantity/' , responseBody : '15' } ,
38+ { path : '/path/to/endpoint/not/being/used/' , multipleResponses : [
39+ { responseBody : { value : 'I will not be used' } } ,
40+ { responseBody : { value : 'Me neither' } } ,
41+ ] } ,
42+ ] )
43+ . mount ( )
44+
45+ await wait ( ( ) => {
46+ getNotUtilizedResponses ( )
47+ expect ( consoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'the following responses are not being used:' ) )
48+ expect ( consoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( '/path/to/endpoint/not/being/used/' ) )
49+ expect ( consoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'get' ) )
50+ expect ( consoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'I will not be used' ) )
51+ expect ( consoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Me neither' ) )
52+ } )
53+ } )
54+
55+ it ( 'should warn when at least one of the multiple responses are not being used' , async ( ) => {
56+ const consoleWarn = jest . spyOn ( console , 'warn' ) . mockImplementation ( )
57+ const productsBeforeRefreshing = [ 'tomato' , 'orange' ]
58+ const productsAfterRefreshing = [ 'tomato' , 'orange' , 'apple' ]
59+
60+ const { container } = wrap ( MyComponentRepeatingHttpCalls )
61+ . withMocks ( { path : '/path/to/get/products/' , multipleResponses : [
62+ { responseBody : productsBeforeRefreshing } ,
63+ { responseBody : productsAfterRefreshing } ,
64+ ] } , )
65+ . mount ( )
66+
67+ refreshProductsList ( container )
68+
69+ await wait ( ( ) => {
70+ getNotUtilizedResponses ( )
71+ expect ( consoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'the following responses are not being used:' ) )
72+ expect ( consoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( '/path/to/get/products/' ) )
73+ expect ( consoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'get' ) )
74+ expect ( consoleWarn ) . not . toHaveBeenCalledWith ( expect . stringContaining ( JSON . stringify ( productsBeforeRefreshing ) ) )
75+ expect ( consoleWarn ) . toHaveBeenCalledWith ( expect . stringContaining ( JSON . stringify ( productsAfterRefreshing ) ) )
76+ } )
77+ } )
78+
79+ it ( 'should not warn when all the responses are being used' , async ( ) => {
80+ const consoleWarn = jest . spyOn ( console , 'warn' )
81+
82+ wrap ( MyComponentMakingHttpCalls )
83+ . withMocks ( { path : '/path/to/get/quantity/' , responseBody : '15' } )
84+ . mount ( )
85+
86+ await wait ( ( ) => {
87+ getNotUtilizedResponses ( )
88+ expect ( consoleWarn ) . not . toHaveBeenCalled ( )
89+ } )
90+ } )
91+
92+ it ( 'should not warn when all the multiple responses are being used' , async ( ) => {
93+ const consoleWarn = jest . spyOn ( console , 'warn' )
94+ const productsBeforeRefreshing = [ 'tomato' , 'orange' ]
95+
96+ const { container } = wrap ( MyComponentRepeatingHttpCalls )
97+ . withMocks ( { path : '/path/to/get/products/' , multipleResponses : [
98+ { responseBody : productsBeforeRefreshing } ,
99+ ] } , )
100+ . mount ( )
101+
102+ refreshProductsList ( container )
103+
104+ await wait ( ( ) => {
105+ getNotUtilizedResponses ( )
106+ expect ( consoleWarn ) . not . toHaveBeenCalled ( )
107+ } )
108+ } )
0 commit comments