Skip to content

Instantly share code, notes, and snippets.

@gdekefir
Last active January 26, 2018 04:25
Show Gist options
  • Save gdekefir/b1b9a9ff186ec6d812ed6fcc4107bd3e to your computer and use it in GitHub Desktop.
Save gdekefir/b1b9a9ff186ec6d812ed6fcc4107bd3e to your computer and use it in GitHub Desktop.
Using jest-json-schema
const moment = require('moment');
const { URL_SESSION_HISTORY } = require('../../src/constants/urls');
const { STATUS_OK } = require('../../src/constants/used-http-status-codes');
const mocker = require('../../dev-helpers/testing/mocker');
const handler = mocker((req, res) =>
res.status(STATUS_OK).send({
sessions: [
{
id: 5433,
starts_at: moment()
.utc()
.subtract({
hours: 4,
minutes: 24,
seconds: 37
})
.toISOString(),
durationSeconds: moment.duration(1, 'hours').asSeconds(),
adminEdited: false
},
{
id: 567,
starts_at: moment()
.utc()
.subtract({
hours: 2,
minutes: 24,
seconds: 37
})
.toISOString(),
durationSeconds: moment.duration(1, 'hours').asSeconds(),
adminEdited: false
}
]
})
);
const sessionHistory = app => app.get(URL_SESSION_HISTORY, handler);
module.exports = { sessionHistory, handler };
const request = require('supertest');
const express = require('express');
const {matchers} = require('jest-json-schema');
const { URL_SESSION_HISTORY } = require('../../src/constants/urls');
const itTriggersEndpointHandler = require('../../dev-helpers/testing/it-triggers-endpoint-handler');
expect.extend(matchers);
describe('sessionHistory', () => {
const { sessionHistory, handler } = require('./session-history');
const app = sessionHistory(express());
itTriggersEndpointHandler(app, handler, URL_SESSION_HISTORY, `calls handler`);
it(`gives expected response`, done => {
const schema = {
properties: {
sessions: {
type: 'array',
items: {
properties: {
id: {
type: 'number'
},
starts_at: {
type: 'string',
format: "date-time"
},
durationSeconds: {
type: 'number'
},
adminEdited: {
type: 'boolean'
},
},
required: ['id', 'starts_at', 'durationSeconds', 'adminEdited']
},
},
},
required: ['sessions']
};
request(app)
.get(URL_SESSION_HISTORY)
.expect(({ text }) => {
expect(JSON.parse(text)).toMatchSchema(schema);
})
.end(done);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment