File size: 8,877 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
import {
getData,
getError,
getProgress,
dispatchRequest,
reducer,
trackRequests,
} from '../utils.js';
describe( 'WPCOM HTTP Data Layer', () => {
const withData = ( data ) => ( { type: 'SLUGGER', meta: { dataLayer: { data } } } );
const withError = ( error ) => ( { type: 'SLUGGER', meta: { dataLayer: { error } } } );
const withProgress = ( progress ) => ( {
type: 'UPLOAD_PROGRESS',
meta: { dataLayer: { progress } },
} );
describe( '#getData', () => {
test( 'should return successful response data if available', () => {
const data = { utterance: 'Bork bork' };
expect( getData( withData( data ) ) ).toEqual( data );
expect( getData( withData( [] ) ) ).toEqual( [] );
} );
test( 'should return undefined if no response data available', () => {
const action = { type: 'SLUGGER' };
expect( getData( action ) ).toBeUndefined();
} );
test( 'should return valid-but-falsey data', () => {
expect( getData( withData( '' ) ) ).toBe( '' );
expect( getData( withData( null ) ) ).toBe( null );
expect( getData( withData( 0 ) ) ).toBe( 0 );
expect( getData( withData( false ) ) ).toBe( false );
} );
} );
describe( '#getError', () => {
test( 'should return failing error data if available', () => {
const error = { utterance: 'Bork bork' };
expect( getError( withError( error ) ) ).toEqual( error );
} );
test( 'should return undefined if no error data available', () => {
const action = { type: 'SLUGGER' };
expect( getError( action ) ).toBeUndefined();
} );
test( 'should return valid-but-falsey data', () => {
expect( getError( withError( '' ) ) ).toBe( '' );
expect( getError( withError( null ) ) ).toBe( null );
expect( getError( withError( 0 ) ) ).toBe( 0 );
expect( getError( withError( false ) ) ).toBe( false );
} );
} );
describe( '#getProgress', () => {
test( 'should return progress data if available', () => {
const progress = { total: 1234, loaded: 123 };
expect( getProgress( withProgress( progress ) ) ).toEqual( progress );
} );
test( 'should return valid-but-falsey data', () => {
expect( getProgress( withProgress( '' ) ) ).toBe( '' );
expect( getProgress( withProgress( null ) ) ).toBe( null );
expect( getProgress( withProgress( 0 ) ) ).toBe( 0 );
expect( getProgress( withProgress( false ) ) ).toBe( false );
} );
} );
describe( '#reducer', () => {
test( 'should update based on presence of request meta key', () =>
expect(
reducer( {}, { meta: { dataLayer: { status: 'pending', requestKey: 'cats' } } } )
).toMatchObject( {
cats: { status: 'pending' },
} ) );
test( 'should only update requested meta key', () => {
const prev = { cats: { status: 'pending' } };
const action = { meta: { dataLayer: { status: 'failure', requestKey: 'dogs' } } };
expect( reducer( prev, action ) ).toMatchObject( {
cats: { status: 'pending' },
dogs: { status: 'failure' },
} );
} );
test( 'should not update just because an action has meta', () =>
expect( reducer( {}, { meta: { value: 1337 } } ) ).toEqual( {} ) );
test( 'should track previous request when updating', () => {
const prev = { dogs: { status: 'success', lastUpdated: 1000 } };
const action = {
meta: { dataLayer: { requestKey: 'dogs', status: 'pending', pendingSince: 1500 } },
};
const next = reducer( prev, action );
expect( next ).toMatchObject( { dogs: { lastUpdated: 1000 } } );
} );
} );
describe( '#trackRequests', () => {
test( 'should bypass actions without opt-in flag', () => {
const store = {};
const action = {};
const next = jest.fn();
trackRequests( next )( store, action );
expect( next ).toHaveBeenCalledWith( store, action );
} );
test( 'should bypass progress events', () => {
const store = {};
const action = { meta: { dataLayer: { trackRequest: true, progress: {} } } };
const next = jest.fn();
trackRequests( next )( store, action );
expect( next ).toHaveBeenCalledWith( store, action );
} );
test( 'should inject false dispatcher', () => {
const store = { dispatch: jest.fn() };
const action = { meta: { dataLayer: { trackRequest: true } } };
const next = jest.fn();
trackRequests( next )( store, action );
const { dispatch } = next.mock.calls[ 0 ][ 0 ];
dispatch( {} );
const args = store.dispatch.mock.calls[ 0 ];
expect( args ).toHaveLength( 1 );
expect( args[ 0 ] ).toMatchObject( { meta: { dataLayer: {} } } );
} );
} );
describe( '#dispatchRequest', () => {
const fetch = () => ( { type: 'REQUEST' } );
const onSuccess = ( action, data ) => ( { type: 'SUCCESS', data } );
const onError = ( action, error ) => ( { type: 'FAILURE', error } );
const onProgress = ( action, progress ) => ( { type: 'PROGRESS', progress } );
const dispatcher = dispatchRequest( {
fetch,
onSuccess,
onError,
onProgress,
} );
const data = { count: 5 };
const dataEmptyString = '';
const dataEmptyArray = [];
const error = { message: 'oh no!' };
const progress = { loaded: 45, total: 80 };
const fetchHttpAction = { type: 'REFILL' };
const successHttpAction = { type: 'REFILL', meta: { dataLayer: { data } } };
const successEmptyStringHttpAction = {
type: 'REFILL',
meta: { dataLayer: { data: dataEmptyString } },
};
const successEmptyArrayHttpAction = {
type: 'REFILL',
meta: { dataLayer: { data: dataEmptyArray } },
};
const failureHttpAction = { type: 'REFILL', meta: { dataLayer: { error } } };
const progressHttpAction = { type: 'REFILL', meta: { dataLayer: { progress } } };
const bothHttpAction = { type: 'REFILL', meta: { dataLayer: { data, error } } };
const fetchAction = { type: 'REQUEST' };
const successAction = { type: 'SUCCESS', data };
const successEmptyStringAction = { type: 'SUCCESS', data: dataEmptyString };
const successEmptyArrayAction = { type: 'SUCCESS', data: dataEmptyArray };
const failureAction = { type: 'FAILURE', error };
const progressAction = { type: 'PROGRESS', progress };
let dispatch;
let store;
beforeEach( () => {
dispatch = jest.fn();
store = { dispatch };
} );
test( 'should initiate request if meta information missing', () => {
dispatcher( store, fetchHttpAction );
expect( dispatch ).toHaveBeenCalledWith( fetchAction );
} );
test( 'should call onSuccess if meta includes response data', () => {
dispatcher( store, successHttpAction );
expect( dispatch ).toHaveBeenCalledWith( successAction );
} );
test( 'should call onSuccess if meta includes empty string response data', () => {
dispatcher( store, successEmptyStringHttpAction );
expect( dispatch ).toHaveBeenCalledWith( successEmptyStringAction );
} );
test( 'should call onSuccess if meta includes empty array response data', () => {
dispatcher( store, successEmptyArrayHttpAction );
expect( dispatch ).toHaveBeenCalledWith( successEmptyArrayAction );
} );
test( 'should call onError if meta includes error data', () => {
dispatcher( store, failureHttpAction );
expect( dispatch ).toHaveBeenCalledWith( failureAction );
} );
test( 'should call onError if meta includes both response data and error data', () => {
dispatcher( store, bothHttpAction );
expect( dispatch ).toHaveBeenCalledWith( failureAction );
} );
test( 'should call onProgress if meta includes progress data', () => {
dispatcher( store, progressHttpAction );
expect( dispatch ).toHaveBeenCalledWith( progressAction );
} );
test( 'should not throw runtime error if onProgress is not specified', () => {
expect( () => {
dispatchRequest( { fetch, onSuccess, onError } )( store, progressHttpAction );
} ).not.toThrow( TypeError );
} );
test( 'should pass data to fromApi for validation', () => {
const fromApi = jest.fn( ( input ) => input );
dispatchRequest( { fetch, onSuccess, onError, fromApi } )( store, successHttpAction );
expect( fromApi ).toHaveBeenCalledWith( successHttpAction.meta.dataLayer.data );
expect( dispatch ).toHaveBeenCalledWith( successAction );
} );
test( 'should fail-over on invalid response data', () => {
const fromApi = () => {
throw new Error( 'Test schema error' );
};
dispatchRequest( { fetch, onSuccess, onError, fromApi } )( store, successHttpAction );
const args = dispatch.mock.calls[ 0 ];
expect( args ).toHaveLength( 1 );
expect( args[ 0 ] ).toMatchObject( { type: 'FAILURE' } );
} );
test( 'should return result of fromApi', () => {
const fromApi = ( input ) => `Hello, ${ input }!`;
const action = {
type: 'REFILL',
meta: { dataLayer: { data: 'world' } },
};
dispatchRequest( { fetch, onSuccess, onError, fromApi } )( store, action );
expect( dispatch ).toHaveBeenCalledWith( {
type: 'SUCCESS',
data: 'Hello, world!',
} );
} );
} );
} );
|