Skip to content

Commit

Permalink
feat: expose createCallRecord (#26)
Browse files Browse the repository at this point in the history
* feat: expose CallRecord.create

Use this function to create an instance of CallRecord in memory.
On top of the data, a CallRecord instance has a `tersify()` method to ensure when stringifing it using `tersify()`, all data in the CallRecord will be preserved.

* fix: CallRecord.create

Change the expose to CallRecord.create.
And fix tersifing it.

* fix: reference
  • Loading branch information
unional authored Jan 11, 2018
1 parent 19e4218 commit e21f7fe
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 58 deletions.
20 changes: 20 additions & 0 deletions src/CallEntry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { CallRecord } from './CallRecord'

export interface CallEntry extends Promise<any> {
inputs: any[],
/**
* Synchronous result.
*/
output: any,
/**
* Synchronous error got thrown.
*/
error: any,
getCallRecord(): Promise<CallRecord>
}

// export const CallEntry = {
// create(callRecord: CallRecord) {
// return {}
// }
// }
25 changes: 25 additions & 0 deletions src/CallRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { tersible, tersify } from 'tersify'

export interface CallRecord {
inputs: any[],
output: any,
error: any,
asyncOutput: any,
asyncError: any,
tersify(): string
}

export const CallRecord = {
/**
* Creates a call record object.
*/
create({ inputs, output, error, asyncOutput, asyncError }: CallRecord) {
return tersible({ inputs, output, error, asyncOutput, asyncError },
() => {
const obj = { inputs, output, error } as CallRecord
if (asyncError !== undefined) obj.asyncError = asyncError
if (asyncOutput !== undefined) obj.asyncOutput = asyncOutput
return tersify(obj, { maxLength: Infinity })
})
}
}
35 changes: 8 additions & 27 deletions src/createCallRecordCreator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { tersify, tersible } from 'tersify'

import { CallEntry } from './interfaces'
import { CallRecord } from './CallRecord'
import { CallEntry } from './CallEntry'

export function createCallRecordCreator(args: any[]) {
let resolve
Expand All @@ -13,31 +12,13 @@ export function createCallRecordCreator(args: any[]) {
inputs: args,
getCallRecord() {
return callEntry.then(asyncOutput => {
const { inputs, output, error } = callEntry
return tersible({
inputs,
output,
error,
asyncOutput
}, () => tersify({
inputs,
output,
error,
asyncOutput
}, { maxLength: Infinity }))
return CallRecord.create({
...callEntry, asyncOutput
})
}, asyncError => {
const { inputs, output, error } = callEntry
return tersible({
inputs,
output,
error,
asyncError
}, () => tersify({
inputs,
output,
error,
asyncError
}, { maxLength: Infinity }))
return CallRecord.create({
...callEntry, asyncError
})
})
}
}) as CallEntry
Expand Down
2 changes: 1 addition & 1 deletion src/createSatisfier.exec.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test } from 'ava'

import { createSatisfier } from './index'
import { assertExec, assertRegExp } from './testUtil';
import { assertExec, assertRegExp } from './testUtil'

test('primitive types without specifing generic will work without issue.', t => {
t.is(createSatisfier(1).exec(1), undefined)
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './CallRecord'
export * from './createSatisfier'
export * from './interfaces'
export * from './isInRange'
Expand Down
22 changes: 1 addition & 21 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
import { Tersible } from 'tersify'

export interface CallRecord {
inputs: any[],
output: any,
error: any,
asyncOutput: any,
asyncError: any,
tersify(): string
}

export interface CallEntry extends Promise<any> {
inputs: any[],
/**
* Synchronous result.
*/
output: any,
/**
* Synchronous error got thrown.
*/
error: any,
getCallRecord(): Promise<CallRecord>
}
import { CallEntry } from './CallEntry'

export interface Spec<T extends Function> {
spiedFn: T,
Expand Down
24 changes: 16 additions & 8 deletions src/spy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { test } from 'ava'

import { spy } from './index'
import { tersify } from 'tersify';

function increment(x: number) { return ++x }

Expand Down Expand Up @@ -32,7 +31,7 @@ test('tersify for sync call', async t => {

t.is(fn(1), 2)
const record = await calls[0].getCallRecord()
t.is(record.tersify(), `{ inputs: [1], output: 2, error: undefined, asyncOutput: undefined }`)
t.is(record.tersify(), `{ inputs: [1], output: 2, error: undefined }`)
})

// this is not a valid test as the package is used for boundary testing.
Expand Down Expand Up @@ -79,7 +78,9 @@ test('then() will receive result from promise', async t => {
const { fn, calls } = spy(resolve)
// tslint:disable-next-line
fn(1)
t.is(await calls[0], 1)
return calls[0].then(actual => {
t.is(actual, 1)
})
})

test('result from promise can be retrieved from await on the call', async t => {
Expand All @@ -101,16 +102,23 @@ test('catch() will receive error thrown by promise', async t => {
})
})

test('tersify for sync call', async t => {
const { fn, calls } = spy(reject)
test('tersify for resolve call', async t => {
const { fn, calls } = spy(resolve)
// tslint:disable-next-line
fn(1)
return calls[0].getCallRecord()
.then(record => {
t.is(record.tersify(), `{ inputs: [1], output: {}, error: undefined, asyncOutput: 1 }`)
})
})


return fn(1).catch(actualError => {
console.log(tersify(actualError))
test('tersify for reject call', async t => {
const { fn, calls } = spy(reject)
return fn(1).catch(() => {
return calls[0].getCallRecord()
.then(record => {
t.is(record.tersify(), `{ inputs: [1], output: {}, error: undefined, asyncError: { message: '1' } }`)
})
})
})

2 changes: 1 addition & 1 deletion src/spy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CallEntry } from './CallEntry'
import { createCallRecordCreator } from './createCallRecordCreator'
import { CallEntry } from './interfaces'

export interface Spy<T> {
calls: ReadonlyArray<CallEntry>,
Expand Down

0 comments on commit e21f7fe

Please sign in to comment.