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
Rewrites simple modules to TypeScript #1545
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
866500f
refactor: rewrites "isURL" to TypeScript
artem-zakharchenko f2a044a
refactor: adds basic types in "general.d.ts"
artem-zakharchenko bf43084
refactor: rewrites "resolveLocations" to TypeScript
artem-zakharchenko 57ef264
refactor: rewrites "resolveModule" to TypeScript
artem-zakharchenko 25e757a
refactor: rewrites "resolvePaths" to TypeScript
artem-zakharchenko 5196ccb
refactor: rewrites "sortTransactions" to TypeScript
artem-zakharchenko a6071e3
chore: sets rootDir of unit tests to the root dir of monorepo
artem-zakharchenko bdd0c21
test: adds new "sortTransactions" unit test
artem-zakharchenko 875fc45
fix: uses "HTTPMethod" enum over "RESTMethod"
artem-zakharchenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,82 @@ | ||
export enum HTTPMethod { | ||
CONNECT = 'CONNECT', | ||
OPTIONS = 'OPTIONS', | ||
POST = 'POST', | ||
GET = 'GET', | ||
HEAD = 'HEAD', | ||
PUT = 'PUT', | ||
PATCH = 'PATCH', | ||
DELETE = 'DELETE', | ||
TRACE = 'TRACE', | ||
} | ||
|
||
export enum BodyEncoding { | ||
'utf-8', | ||
'base64', | ||
} | ||
|
||
export enum TransactionTestStatus { | ||
'pass', | ||
'fail', | ||
'skip', | ||
} | ||
|
||
export interface Transaction { | ||
id: string; | ||
name: string; | ||
origin: TransactionOrigin; | ||
host: string; | ||
port: number; | ||
protocol: 'http:' | 'https:'; | ||
fullPath: string; | ||
request: TransactionRequest; | ||
expected: { | ||
statusCode: number; | ||
headers: Record<string, string>; | ||
body: string; | ||
bodySchema: Record<string, any>; | ||
}; | ||
real: { | ||
statusCode: string; | ||
headers: Record<string, string>; | ||
body: string; | ||
bodyEncoding: BodyEncoding; | ||
}; | ||
skip: boolean; | ||
fail: boolean; | ||
|
||
test: TransactionTest; | ||
} | ||
|
||
export interface TransactionRequest { | ||
method: HTTPMethod; | ||
url: string; | ||
body?: string; | ||
bodyEncoding?: BodyEncoding; | ||
headers?: Record<string, string>; | ||
} | ||
|
||
export interface TransactionOrigin { | ||
filename: string; | ||
apiName: string; | ||
resourceGroupName: string; | ||
resourceName: string; | ||
actionName: string; | ||
exampleName: string; | ||
} | ||
|
||
export interface TransactionTest { | ||
start: Date; | ||
end: Date; | ||
duration: number; | ||
startedAt: number; | ||
title: string; | ||
request: TransactionRequest; | ||
actual: any; | ||
expected: any; | ||
status: TransactionTestStatus; | ||
message: string; | ||
results: any; | ||
valid: boolean; | ||
origin: TransactionOrigin; | ||
} |
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 |
---|---|---|
@@ -1,8 +1,6 @@ | ||
/** | ||
* Decides whether given string is a URL or not | ||
* @param {string} location | ||
* @returns {boolean} | ||
*/ | ||
export default function isURL(location) { | ||
export default function isURL(location: string): boolean { | ||
return /^http(s)?:\/\//.test(location); | ||
} |
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
5 changes: 4 additions & 1 deletion
5
packages/dredd/lib/resolveModule.js β packages/dredd/lib/resolveModule.ts
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
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
This file was deleted.
Oops, something went wrong.
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,61 @@ | ||
import { HTTPMethod, Transaction } from './general'; | ||
|
||
const sortedMethods: HTTPMethod[] = [ | ||
HTTPMethod.CONNECT, | ||
HTTPMethod.OPTIONS, | ||
HTTPMethod.POST, | ||
HTTPMethod.GET, | ||
HTTPMethod.HEAD, | ||
HTTPMethod.PUT, | ||
HTTPMethod.PATCH, | ||
HTTPMethod.DELETE, | ||
HTTPMethod.TRACE, | ||
]; | ||
|
||
// Often, API description is arranged with a sequence of methods that lends | ||
// itself to understanding by the human reading the documentation. | ||
// | ||
// However, the sequence of methods may not be appropriate for the machine | ||
// reading the documentation in order to test the API. | ||
// | ||
// By sorting the transactions by their methods, it is possible to ensure that | ||
// objects are created before they are read, updated, or deleted. | ||
export default function sortTransactions( | ||
transactions: Transaction[], | ||
): Transaction[] { | ||
// Convert the list of transactions into a list of tuples | ||
// that hold each trasnaction index and details. | ||
const tempTransactions: Array<[number, Transaction]> = transactions.map( | ||
(transaction, index) => [index, transaction], | ||
); | ||
|
||
tempTransactions.sort( | ||
([leftIndex, leftTransaction], [rightIndex, rightTransaction]) => { | ||
const methodIndexA = sortedMethods.indexOf( | ||
leftTransaction.request.method, | ||
); | ||
const methodIndexB = sortedMethods.indexOf( | ||
rightTransaction.request.method, | ||
); | ||
|
||
// Sort transactions according to the transaction's request method | ||
if (methodIndexA < methodIndexB) { | ||
return -1; | ||
} | ||
|
||
if (methodIndexA > methodIndexB) { | ||
return 1; | ||
} | ||
|
||
// In case two transactions' request methods are the same, | ||
// preserve the original order of those transactions | ||
return leftIndex - rightIndex; | ||
}, | ||
); | ||
|
||
const cleanTransactions = tempTransactions.map( | ||
([_, transaction]) => transaction, | ||
); | ||
|
||
return cleanTransactions; | ||
} |
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
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, HTTPMethod } from '../../lib/general'; | ||
|
||
const createTransaction = (transaction: Partial<Transaction>) => { | ||
return R.mergeDeepRight<Partial<Transaction>>({ | ||
protocol: 'http:', | ||
host: 'localhost', | ||
})(transaction); | ||
}; | ||
|
||
const transactions = Object.keys(HTTPMethod).reduce< | ||
Record<HTTPMethod, Transaction> | ||
>( | ||
(acc, method: HTTPMethod) => { | ||
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: HTTPMethod.GET, | ||
url: '/endpoint', | ||
}, | ||
}); | ||
|
||
const getTwo = createTransaction({ | ||
id: 'two', | ||
request: { | ||
method: HTTPMethod.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); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a similar test written in JS being removed. Does it mean you want to keep it to be sure nothing got broken, or that it doesn't exist and this part isn't tested?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find any existing test that would cover
sortTransactions
. To my knowledge this adds the missing unit test, so there is no JavaScript predecessor to remove. Please, point me if you know where this behavior may be tested already. Thanks.