Skip to content

Commit

Permalink
test: ✅ migrate sort test to es6
Browse files Browse the repository at this point in the history
  • Loading branch information
rijkerd committed Jun 5, 2021
1 parent 21ebdab commit b322e19
Showing 1 changed file with 91 additions and 124 deletions.
215 changes: 91 additions & 124 deletions test/unit/parser.sort.spec.js
Original file line number Diff line number Diff line change
@@ -1,161 +1,134 @@
'use strict';
import chai from 'chai';
import express from 'express';
import request from 'supertest';
import * as parser from '../../src';

const { expect } = chai;

//global imports
const path = require('path');
const chai = require('chai');
const express = require('express');
const request = require('supertest');
const expect = chai.expect;
const parser = require(path.join(__dirname, '..', '..', 'lib', 'parser'));

describe('sort', function () {

it('should be a function', function () {
describe('sort', () => {
it('should be a function', () => {
expect(parser.sort).to.exist;
expect(parser.sort).to.be.a('function');
expect(parser.sort.name).to.be.equal('sort');
expect(parser.sort.length).to.be.equal(2);
});

it('should parse json based sort', function (done) {
it('should parse json based sort', (done) => {
const _sort = { name: 1, email: 1 };
const query = { sort: _sort };

parser
.sort(JSON.stringify(query), function (error, sort) {
expect(error).to.not.exist;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, sort);
});

parser.sort(JSON.stringify(query), (error, sort) => {
expect(error).to.not.exist;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, sort);
});
});


it('should parse json based sort', function (done) {
it('should parse json based sort', (done) => {
const _sort = { name: 'desc', email: 'asc' };
const query = { sort: _sort };

parser
.sort(JSON.stringify(query), function (error, sort) {
expect(error).to.not.exist;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, sort);
});

parser.sort(JSON.stringify(query), (error, sort) => {
expect(error).to.not.exist;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, sort);
});
});


it('should parse json based sort', function (done) {
it('should parse json based sort', (done) => {
const _sort = { name: 'descending', email: 'ascending' };
const query = { sort: _sort };

parser
.sort(JSON.stringify(query), function (error, sort) {
expect(error).to.not.exist;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, sort);
});

parser.sort(JSON.stringify(query), (error, sort) => {
expect(error).to.not.exist;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, sort);
});
});


it('should parse object based sort', function (done) {
it('should parse object based sort', (done) => {
const _sort = { name: 1, email: 1 };
const query = { sort: _sort };

parser
.sort(query, function (error, sort) {
expect(error).to.not.exist;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, sort);
});

parser.sort(query, (error, sort) => {
expect(error).to.not.exist;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, sort);
});
});

it('should parse object based sort', function (done) {
it('should parse object based sort', (done) => {
const _sort = { name: 'desc', email: 'asc' };
const query = { sort: _sort };

parser
.sort(query, function (error, sort) {
expect(error).to.not.exist;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, sort);
});

parser.sort(query, (error, sort) => {
expect(error).to.not.exist;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, sort);
});
});

it('should parse object based sort', function (done) {
it('should parse object based sort', (done) => {
const _sort = { name: 'descending', email: 'ascending' };
const query = { sort: _sort };

parser
.sort(query, function (error, sort) {
expect(error).to.not.exist;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, sort);
});

parser.sort(query, (error, sort) => {
expect(error).to.not.exist;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, sort);
});
});

it('should parse string based sort', function (done) {
it('should parse string based sort', (done) => {
const _sort = { name: 1, email: 1 };
const query = { sort: 'name,email' };

parser
.sort(JSON.stringify(query), function (error, sort) {
expect(error).to.not.exist;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, sort);
});

parser.sort(JSON.stringify(query), (error, sort) => {
expect(error).to.not.exist;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, sort);
});
});


it('should parse string based sort', function (done) {
it('should parse string based sort', (done) => {
const _sort = { name: -1, email: -1 };
const query = { sort: '-name,-email' };

parser
.sort(query, function (error, projection) {
expect(error).to.not.exist;
expect(projection).to.exist;
expect(projection).to.eql(_sort);
done(error, projection);
});

parser.sort(query, (error, projection) => {
expect(error).to.not.exist;
expect(projection).to.exist;
expect(projection).to.eql(_sort);
done(error, projection);
});
});

describe('http', function () {

describe('http', () => {
const app = express();
app.use('/sort', function (request, response) {
parser
.sort(request.query, function (error, sorts) {
if (error) {
throw error;
} else {
response.json(sorts);
}
});
app.use('/sort', (req, response) => {
parser.sort(req.query, (error, sorts) => {
if (error) {
throw error;
} else {
response.json(sorts);
}
});
});

it('should parse json based sort', function (done) {
it('should parse json based sort', (done) => {
const _sort = { name: 1, email: 1 };
const query = { sort: _sort };

request(app)
.get('/sort')
.query(query)
.expect(200, function (error, response) {
.expect(200, (error, response) => {
expect(error).to.not.exist;
const sort = response.body;
expect(sort).to.exist;
Expand All @@ -164,15 +137,14 @@ describe('sort', function () {
});
});


it('should parse json based sort', function (done) {
it('should parse json based sort', (done) => {
const _sort = { name: 'desc', email: 'asc' };
const query = { sort: _sort };

request(app)
.get('/sort')
.query(query)
.expect(200, function (error, response) {
.expect(200, (error, response) => {
expect(error).to.not.exist;
const sort = response.body;
expect(sort).to.exist;
Expand All @@ -181,15 +153,14 @@ describe('sort', function () {
});
});


it('should parse json based sort', function (done) {
it('should parse json based sort', (done) => {
const _sort = { name: 'descending', email: 'ascending' };
const query = { sort: _sort };

request(app)
.get('/sort')
.query(query)
.expect(200, function (error, response) {
.expect(200, (error, response) => {
expect(error).to.not.exist;
const sort = response.body;
expect(sort).to.exist;
Expand All @@ -198,14 +169,14 @@ describe('sort', function () {
});
});

it('should parse string based sort', function (done) {
it('should parse string based sort', (done) => {
const _sort = { name: 1, email: 1 };
const query = { sort: 'name,email' };

request(app)
.get('/sort')
.query(query)
.expect(200, function (error, response) {
.expect(200, (error, response) => {
expect(error).to.not.exist;
const sort = response.body;
expect(sort).to.exist;
Expand All @@ -214,15 +185,14 @@ describe('sort', function () {
});
});


it('should parse string based sort', function (done) {
it('should parse string based sort', (done) => {
const _sort = { name: -1, email: -1 };
const query = { sort: '-name,-email' };

request(app)
.get('/sort')
.query(query)
.expect(200, function (error, response) {
.expect(200, (error, response) => {
expect(error).to.not.exist;
const sort = response.body;
expect(sort).to.exist;
Expand All @@ -231,14 +201,14 @@ describe('sort', function () {
});
});

it('should parse string based sort', function (done) {
it('should parse string based sort', (done) => {
const _sort = { name: -1, email: 1 };
const query = { sort: '-name,email' };

request(app)
.get('/sort')
.query(query)
.expect(200, function (error, response) {
.expect(200, (error, response) => {
expect(error).to.not.exist;
const sort = response.body;
expect(sort).to.exist;
Expand All @@ -247,12 +217,12 @@ describe('sort', function () {
});
});

it('should parse string based sort', function (done) {
it('should parse string based sort', (done) => {
const _sort = { name: -1, email: 1 };

request(app)
.get('/sort?sort[name]=-1&sort[email]=1')
.expect(200, function (error, response) {
.expect(200, (error, response) => {
expect(error).to.not.exist;
const sort = response.body;
expect(sort).to.exist;
Expand All @@ -261,12 +231,12 @@ describe('sort', function () {
});
});

it('should parse string based sort', function (done) {
it('should parse string based sort', (done) => {
const _sort = { name: -1, email: 1 };

request(app)
.get('/sort?sort=-name,email')
.expect(200, function (error, response) {
.expect(200, (error, response) => {
expect(error).to.not.exist;
const sort = response.body;
expect(sort).to.exist;
Expand All @@ -275,21 +245,18 @@ describe('sort', function () {
});
});


it('should parse json based sort', function (done) {
it('should parse json based sort', (done) => {
const _sort = { name: 1, email: 1 };

request(app)
.get('/sort?sort={"name": "1", "email": "1"}')
.expect(200, function (error, response) {
.expect(200, (error, response) => {
expect(error).to.not.exist;
const sort = response.body;
expect(sort).to.exist;
expect(sort).to.eql(_sort);
done(error, response);
});
});

});

});
});

0 comments on commit b322e19

Please sign in to comment.