/two-faced

A client-side, test server focused on strong typing

Primary LanguageTypeScript

A simple example:

First setup some routes for your server.

// routes.js
export default (server) => {
  server.get('/posts/:id', (db, request) => {
    return db.posts.find(request.params.id);
  });
}

You'll also want to create a factory to setup default properties in your tests.

// factories/user.js
export default (attrs, i) => ({
  attrs: {
    name: `name ${i}`,
    age: 23
  }
});

Adapting a hypothetical interface.

export function connectFetch(server) {
  window.fetch = function(url, init) {
    const request = new Request(url, init);
    return server.handleRequest(request);
  }
}

Put it all together.

const server = new Server({
  routes(router) {
    router.get('/posts/:id', (db, request) {
      return db.posts.find(request.params.id);
    });
  },
});

connectFetch(server);

const user = server.create('user', { name: 'mitch' });

api.get(`/users/${user.id}`)

server.settle().then(() => {

});
export const attrs = (attrs, i) => ({
  attrs: {
    name: `name ${i}`,
    age: 23
  },
});

export const afterCreate = (user, server) {
  server.create('post', { user });
});

// later in index.js
export * as user from './user';