Last active
June 24, 2019 15:27
-
-
Save KalachevDev/e192dc68769803da22a5412b7af602d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fetch from 'fetch'; | |
import { mockServer } from 'ember-cli-fastboot-testing/test-support'; | |
import { all } 'rsvp'; | |
export default new class MockServer { | |
constructor () { | |
this.namespace = 'mirage-namespace'; | |
this.promises = []; | |
} | |
mock (method, url, options = {}) { | |
this.promises.push(async () => { | |
try { | |
const fetchOptions = { method: method.toUpperCase(), ...options }; | |
const response = await fetch(`${this.namespace}${url}`, fetchOptions); | |
const data = await response.json(); | |
await mockServer[method](url, data, options.status); | |
} catch (e) { | |
this.promises = []; | |
throw e; | |
} | |
}); | |
return this; | |
} | |
async resolve () { | |
await all(this.promises); | |
this.promises = []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment