Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
test: adds new "sortTransactions" unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed Oct 22, 2019
1 parent a6071e3 commit bdd0c21
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions packages/dredd/test/unit/sortTransactions-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import R from 'ramda';
import { expect } from 'chai';
import sortTransactions from '../../lib/sortTransactions';
import { Transaction, RESTMethod } from '../../lib/general';

const createTransaction = (transaction: Partial<Transaction>) => {
return R.mergeDeepRight<Partial<Transaction>>({
protocol: 'http:',
host: 'localhost',
})(transaction);
};

const transactions = Object.keys(RESTMethod).reduce<
Record<RESTMethod, Transaction>
>(
(acc, method: RESTMethod) => {
return R.assoc(
method,
createTransaction({
request: {
url: '/endpoint',
method,
},
}),
acc,
);
},
{} as any,
);

describe('sortTransactions', () => {
describe('given transactions list in arbitrary order', () => {
const sorted = sortTransactions([
transactions.GET,
transactions.TRACE,
transactions.OPTIONS,
transactions.HEAD,
transactions.DELETE,
transactions.POST,
transactions.PATCH,
transactions.PUT,
transactions.CONNECT,
]);

it('should return transactions list sorted', () => {
expect(sorted).to.deep.equal([
transactions.CONNECT,
transactions.OPTIONS,
transactions.POST,
transactions.GET,
transactions.HEAD,
transactions.PUT,
transactions.PATCH,
transactions.DELETE,
transactions.TRACE,
]);
});
});

describe('given multiple transactions with the same method', () => {
const getOne = createTransaction({
id: 'one',
request: {
method: RESTMethod.GET,
url: '/endpoint',
},
});

const getTwo = createTransaction({
id: 'two',
request: {
method: RESTMethod.GET,
url: '/endpoint',
},
});

// This doesn't assert the identity of transactions.
const sorted = sortTransactions([getOne as any, getTwo]);

it('should sort transactions by occurence (asc)', () => {
expect(sorted).to.deep.equal([getOne, getTwo]);
});
});

describe('given transactions list sorted properly', () => {
const transactionsList = [
transactions.CONNECT,
transactions.OPTIONS,
transactions.POST,
transactions.POST,
transactions.GET,
transactions.HEAD,
transactions.PUT,
transactions.PATCH,
transactions.DELETE,
transactions.TRACE,
];
const sorted = sortTransactions(transactionsList);

it('should return transactions list as-is', () => {
expect(sorted).to.deep.equal(transactionsList);
});
});
});

0 comments on commit bdd0c21

Please sign in to comment.