-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@whook/example): add a time mock example
- Loading branch information
Showing
10 changed files
with
179 additions
and
40 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 |
---|---|---|
|
@@ -146,12 +146,12 @@ describe('initCreateWhook', () => { | |
await createWhook(); | ||
|
||
expect( | ||
JSON.parse( | ||
outputFile.mock.calls. | ||
find((call) => call[0].toString().endsWith('package.json'))?.[1]?. | ||
toString() || '' | ||
) | ||
).toMatchInlineSnapshot(` | ||
JSON.parse( | ||
outputFile.mock.calls | ||
.find((call) => call[0].toString().endsWith('package.json'))?.[1] | ||
?.toString() || '', | ||
), | ||
).toMatchInlineSnapshot(` | ||
{ | ||
"author": { | ||
"email": "[email protected]", | ||
|
@@ -344,12 +344,12 @@ describe('initCreateWhook', () => { | |
await createWhook(); | ||
|
||
expect( | ||
JSON.parse( | ||
outputFile.mock.calls. | ||
find((call) => call[0].toString().endsWith('package.json'))?.[1]?. | ||
toString() || '' | ||
) | ||
).toMatchInlineSnapshot(` | ||
JSON.parse( | ||
outputFile.mock.calls | ||
.find((call) => call[0].toString().endsWith('package.json'))?.[1] | ||
?.toString() || '', | ||
), | ||
).toMatchInlineSnapshot(` | ||
{ | ||
"author": { | ||
"email": "[email protected]", | ||
|
@@ -527,12 +527,12 @@ describe('initCreateWhook', () => { | |
await createWhook(); | ||
|
||
expect( | ||
JSON.parse( | ||
outputFile.mock.calls. | ||
find((call) => call[0].toString().endsWith('package.json'))?.[1]?. | ||
toString() || '' | ||
) | ||
).toMatchInlineSnapshot(` | ||
JSON.parse( | ||
outputFile.mock.calls | ||
.find((call) => call[0].toString().endsWith('package.json'))?.[1] | ||
?.toString() || '', | ||
), | ||
).toMatchInlineSnapshot(` | ||
{ | ||
"author": { | ||
"email": "[email protected]", | ||
|
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 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,87 @@ | ||
import { | ||
type WhookAPIHandlerDefinition, | ||
type WhookResponse, | ||
} from '@whook/whook'; | ||
import { type LogService } from 'common-services'; | ||
import { type ClockMockService } from 'application-services'; | ||
import { autoHandler } from 'knifecycle'; | ||
import { YError } from 'yerror'; | ||
import { env } from 'node:process'; | ||
|
||
import { type AppEnv } from '../index.js'; | ||
import { type TimeMockService } from 'application-services/dist/services/timeMock.js'; | ||
|
||
export const definition: WhookAPIHandlerDefinition = { | ||
path: '/time', | ||
method: 'put', | ||
operation: { | ||
'x-whook': { | ||
type: 'http', | ||
disabled: env.APP_ENV !== 'test', | ||
}, | ||
operationId: 'putTime', | ||
summary: 'Allows to set the time like the great random god', | ||
tags: ['system'], | ||
requestBody: { | ||
required: true, | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
time: { type: 'number' }, | ||
isFixed: { type: 'boolean' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
responses: { | ||
200: { | ||
description: 'Success', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'number', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default autoHandler(putTime); | ||
|
||
async function putTime( | ||
{ | ||
APP_ENV, | ||
CLOCK_MOCK, | ||
time, | ||
log, | ||
}: { | ||
APP_ENV: AppEnv; | ||
CLOCK_MOCK: ClockMockService; | ||
time: TimeMockService; | ||
log: LogService; | ||
}, | ||
{ body }: { body: { time: number; isFixed: boolean } }, | ||
): Promise<WhookResponse<201, void, number>> { | ||
if (APP_ENV !== 'local' && APP_ENV !== 'test') { | ||
throw new YError('E_NO_MOCK_IN_PROD', APP_ENV); | ||
} | ||
|
||
CLOCK_MOCK.isFixed = body.isFixed; | ||
CLOCK_MOCK.mockedTime = body.time; | ||
|
||
if (!CLOCK_MOCK.isFixed) { | ||
CLOCK_MOCK.referenceTime = time(); | ||
} | ||
|
||
log('warning', `⌚ - Set time to "${body.time}".`); | ||
|
||
return { | ||
status: 201, | ||
body: time(), | ||
}; | ||
} |
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 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 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