github.com/DerekStrickland/consul@v1.4.5/ui-v2/tests/helpers/repo.js (about) 1 import { get as httpGet } from 'consul-ui/tests/helpers/api'; 2 import { get } from '@ember/object'; 3 import measure from 'consul-ui/tests/helpers/measure'; 4 5 /** Stub an ember-data adapter response using the private method 6 * 7 * Allows you to easily specify a HTTP response for the Adapter. The stub only works 8 * during the 'lifetime' of `cb` and is reset to normal unstubbed functionality afterwards. 9 * 10 * Please Note: This overwrites a private ember-data Adapter method, please understand 11 * the consequences of doing this if you are using it 12 * 13 * @param {function} cb - The callback, or test case, to run using the stubbed response 14 * @param {object} payload - The payload to use as the response 15 * @param {DS.Adapter} adapter - An instance of an ember-data Adapter 16 */ 17 const stubAdapterResponse = function(cb, payload, adapter) { 18 const payloadClone = JSON.parse(JSON.stringify(payload)); 19 const ajax = adapter._ajaxRequest; 20 adapter._ajaxRequest = function(options) { 21 options.success(payloadClone, '200', { 22 status: 200, 23 textStatus: '200', 24 getAllResponseHeaders: function() { 25 return ''; 26 }, 27 }); 28 }; 29 return cb(payload).then(function(result) { 30 adapter._ajaxRequest = ajax; 31 return result; 32 }); 33 }; 34 /** `repo` a helper function to faciliate easy integration testing of ember-data Service 'repo' layers 35 * 36 * Test performance is also measured using `consul-ui/tests/helpers/measure` and therefore results 37 * can optionally be sent to a centralized metrics collection stack 38 * 39 * @param {string} name - The name of your repo Service (only used for meta purposes) 40 * @param {string} payload - The method you are testing (only used for meta purposes) 41 * @param {Service} service - An instance of an ember-data based repo Service 42 * @param {function} stub - A function that receives a `stub` function allowing you to stub 43 * an API endpoint with a set of cookies/env vars used by the double 44 * @param {function} test - Your test case. This function receives an instance of the Service provided 45 * above as a first and only argument, it should return the result of your test 46 * @param {function} assert - Your assertion. This receives the result of the previous function as the first 47 * argument and a function to that receives the stubbed payload giving you an 48 * opportunity to mutate it before returning for use in your assertion 49 */ 50 export default function(name, method, service, stub, test, assert) { 51 const adapter = get(service, 'store').adapterFor(name.toLowerCase()); 52 let tags = {}; 53 const requestHeaders = function(url, cookies = {}) { 54 const key = Object.keys(cookies).find(function(item) { 55 return item.indexOf('COUNT') !== -1; 56 }); 57 tags = { 58 count: typeof key !== 'undefined' ? parseInt(cookies[key]) : 1, 59 }; 60 return httpGet(url, { 61 headers: { 62 cookie: cookies, 63 }, 64 }); 65 }; 66 const parseResponse = function(response) { 67 let actual; 68 if (typeof response.toArray === 'function') { 69 actual = response.toArray().map(function(item) { 70 return get(item, 'data'); 71 }); 72 } else { 73 if (typeof response.get === 'function') { 74 actual = get(response, 'data'); 75 } else { 76 actual = response; 77 } 78 } 79 return actual; 80 }; 81 return stub(requestHeaders).then(function(payload) { 82 return stubAdapterResponse( 83 function(payload) { 84 return measure( 85 function() { 86 return test(service); 87 }, 88 `${name}Service.${method}`, 89 tags 90 ).then(function(response) { 91 assert(parseResponse(response), function(cb) { 92 return cb(payload); 93 }); 94 }); 95 }, 96 payload, 97 adapter 98 ); 99 }); 100 }