UNPKG

thinky-rest

Version:

Create REST resources and controllers with thinky and Express or Restify

87 lines (73 loc) 2.46 kB
'use strict'; var TestFixture = require('./test-fixture'), request = require('request'), expect = require('chai').expect, _ = require('lodash'), rest = require('../lib'), schemas = require('./schemas'); var test = new TestFixture(); describe('Resource(pagination)', function() { before(function() { return test.initializeDatabase() .then(function() { test.models.User = test.db.createModel('users', schemas.User); test.userlist = [ { username: 'arthur', email: 'arthur@gmail.com' }, { username: 'edward', email: 'edward@gmail.com' }, { username: 'henry', email: 'henry@gmail.com' }, { username: 'james', email: 'james@gmail.com' }, { username: 'william', email: 'william@gmail.com' } ]; return test.models.User.tableReady(); }); }); after(function() { return test.dropDatabase(); }); [ { name: 'with default pagination', configuration: {} }, { name: 'without pagination', configuration: { pagination: false } } ].forEach(function(suite) { describe('list ' + suite.name, function() { beforeEach(function() { return test.initializeServer() .then(function() { rest.initialize({ app: test.app, thinky: test.db }); return test.models.User.save(_.cloneDeep(test.userlist)); }) .then(function() { rest.resource(_.extend(suite.configuration, { model: test.models.User })); }); }); afterEach(function(done) { return test.clearDatabase() .then(function() { test.server.close(done); }); }); it('should list records with no criteria', function(done) { request.get({ url: test.baseUrl + '/users' }, function(err, response, body) { expect(response.statusCode).to.equal(200); var records = JSON.parse(body).map(function(r) { delete r.id; return r; }); records = _.sortBy(records, 'username'); expect(records).to.eql(test.userlist); if (!_.has(suite.configuration, 'pagination') || !!suite.configuration.pagination) expect(response.headers['content-range']).to.equal('items 0-4/5'); else expect(response.headers['content-range']).to.not.exist; done(); }); }); }); }); });