Skip to content

Instantly share code, notes, and snippets.

@KalachevDev
Last active June 24, 2019 15:27
Show Gist options
  • Save KalachevDev/e192dc68769803da22a5412b7af602d2 to your computer and use it in GitHub Desktop.
Save KalachevDev/e192dc68769803da22a5412b7af602d2 to your computer and use it in GitHub Desktop.
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