This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: adds new "sortTransactions" unit test
- Loading branch information
1 parent
a6071e3
commit bdd0c21
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |