How to use pretender - 8 common examples

To help you get started, we’ve selected a few pretender examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ember-graphql / ember-apollo-client / tests / dummy / app / instance-initializers / mock-graphql.js View on Github external
// }

    Date() {
      return '2019-09-28';
    },
  };

  let schema = makeExecutableSchema({ typeDefs: schemaString, resolvers });
  addResolveFunctionsToSchema({
    schema,
    resolvers: typeResolvers,
    inheritResolversFromInterfaces: true,
  });
  addMockFunctionsToSchema({ schema, mocks, preserveResolvers: true });

  let pretender = new Pretender();

  pretender.post('https://test.example/graphql', function(request) {
    let body = JSON.parse(request.requestBody);

    return graphql(schema, body.query, {}, {}, body.variables).then(result => {
      return [200, { 'Content-Type': 'application/json' }, result];
    });
  });

  pretender.prepareBody = function(body) {
    return JSON.stringify(body);
  };

  pretender.handledRequest = function(verb, path, request) {
    console.group('Mocked Request');
    console.info(verb, path);
github softlayer / sl-ember-store / tests / dummy / app / initializers / pretender.js View on Github external
export function initialize(/* container, application */) {
    new Pretender(function(){
        this.get( '/foo', function(request){
            var id = request.queryParams.id && ( parseInt( request.queryParams.id ) - 1 ),
                start = request.queryParams.start || 0,
                length = request.queryParams.length || fooRecords.length,
                results = fooRecords.slice( start, length );

            if( request.queryParams.id ){
                return [
                    200,
                    { "Content-Type":"application/json" },
                    JSON.stringify( { foo: fooRecords[ id ] } )
                ];
            }

            return [
                200,
github mike-north / ember-api-actions / tests / dummy / app / routes / index.ts View on Github external
_setupPretender() {
    const server = new Pretender();
    // server.get('/fruits', request => {
    //   return [200, {}, JSON.stringify({
    //     fruits:
    //   })];
    // });
    server.put('/fruits/:id/doRipen', request => {
      const controller = this.get('controller');
      controller.get('requests').addObject({
        url: request.url,
        data: JSON.parse(request.requestBody)
      });
      return [200, {}, '{"status": "ok"}'];
    });
    server.put('/fruits/ripenEverything', request => {
      const controller = this.get('controller');
      controller.get('requests').addObject({
github ember-graphql / ember-apollo-client / tests / helpers / start-pretender.js View on Github external
// }

    Date() {
      return '2019-09-28';
    },
  };

  let schema = makeExecutableSchema({ typeDefs: schemaString, resolvers });
  addResolveFunctionsToSchema({
    schema,
    resolvers: typeResolvers,
    inheritResolversFromInterfaces: true,
  });
  addMockFunctionsToSchema({ schema, mocks, preserveResolvers: true });

  let pretender = new Pretender(function() {
    this.unhandledRequest = function(verb, path) {
      path = decodeURI(path);
      throw `Your Ember app tried to ${verb} '${path}', but there was no route defined to handle this request.`;
    };
  });

  // overriding fetch is required in order to make apollo-client work w/ pretender:
  // https://github.com/pretenderjs/pretender/issues/60
  // https://github.com/apollostack/apollo-client/issues/269
  pretender._ogFetch = window.fetch;
  window.fetch = fetch;

  pretender.originalShutdown = pretender.shutdown;
  pretender.shutdown = function() {
    window.fetch = pretender._ogFetch;
    pretender.originalShutdown(...arguments);
github ember-infinity / ember-infinity / tests / dummy / app / routes / load-previous.js View on Github external
init() {
    this._super(...arguments);
    let fakeData = generateFakeData(104);
    this.set('pretender', new Pretender());
    this.get('pretender').get('/posts', request => {
      let fd = fakeData;
      let page = parseInt(request.queryParams.page, 10);
      let per =  parseInt(request.queryParams.per_page, 10);
      let payload = {
        posts: fd.slice((page - 1) * per, Math.min((page) * per, fd.length)),
        meta: {
          total_pages: Math.ceil(fd.length/per)
        }
      };
      return json(200, payload);

    }, 500 /*ms*/);
  },
github Exelord / ember-custom-actions / tests / dummy / app / services / server.js View on Github external
init() {
    this._super(...arguments);
    this.set('server', new Pretender());
  }
});
github kturney / ember-mapbox-gl / tests / mapbox-server / create.js View on Github external
export default function create() {
  return new Pretender(function() {
    this.get('https://api.mapbox.com/styles/v1/mapbox/streets-v9', function() {
      return [ 200, { 'content-type': 'application/json' }, Style ];
    });

    this.get('https://api.mapbox.com/v4/mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7.json', function() {
      return [ 200, { 'content-type': 'application/json' }, Terrain ];
    });

    this.get('https://api.mapbox.com/styles/v1/mapbox/streets-v9/sprite.json', function() {
      return [ 200, { 'content-type': 'application/json' }, Sprites ];
    });

    this.get('https://api.mapbox.com/styles/v1/mapbox/streets-v9/sprite@2x.json', function() {
      return [ 200, { 'content-type': 'application/json' }, Sprites ];
    });
github danielspaniel / ember-data-factory-guy / addon / mocks / request-manager.js View on Github external
static getPretender() {
    if (!pretender) {
      pretender = new Pretender();
    }
    return pretender;
  }
}

pretender

Pretender is a mock server library for XMLHttpRequest and Fetch, that comes with an express/sinatra style syntax for defining routes and their handlers.

MIT
Latest version published 3 years ago

Package Health Score

59 / 100
Full package analysis

Popular pretender functions

Similar packages