Skip to content

Commit

Permalink
feat(mock): add isMockObject that check if mock object or not
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Dec 14, 2021
1 parent 66da312 commit 87c2ea4
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
8 changes: 7 additions & 1 deletion _e2e/fn_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.
import { expect, fn, test } from "../mod.ts";
import { expect, fn, isMockObject, test } from "../mod.ts";

test("should define implementation as default", () => {
const mockObject = fn().defaultImplementation(() => true);
Expand Down Expand Up @@ -66,3 +66,9 @@ test("should clear mock and all registered once implementations and default", ()
mockObject();
expect(mockObject).toHaveReturnedWith(undefined);
});

test("should be mock object", () => {
const mockObject = fn();
expect(isMockObject(mockObject)).toBeTruthy();
expect(isMockObject({})).toBeFalsy();
});
19 changes: 19 additions & 0 deletions mock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ test("plays video", () => {
});
```

## isMockObject

Whatever argument is `MockObject` or not.

```ts
import {
expect,
fn,
isMockObject,
test,
} from "https://deno.land/x/unitest@$VERSION/mod.ts";

test("should be mock object", () => {
const mockObject = fn();
expect(isMockObject(mockObject)).toBeTruthy();
expect(isMockObject({})).toBeFalsy();
});
```

## mockObject.mock.calls

An array containing the call arguments of all calls that have been made to this
Expand Down
31 changes: 30 additions & 1 deletion mock/fn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.
// This module is browser compatible.

import { isObject } from "../deps.ts";
import { prop } from "../matcher/utils.ts";
import { Mock } from "./mock.ts";
import type { MockSpec } from "./mock.ts";
import { incrementalNumber } from "./utils.ts";
Expand Down Expand Up @@ -328,5 +330,32 @@ function fn(
return call as MockObject;
}

export { fn, MockFnStore };
/** Whatever argument is `MockObject` or not.
* ```ts
* import {
* expect,
* fn,
* isMockObject,
* test,
* } from "https://deno.land/x/unitest@$VERSION/mod.ts";
*
* test("should be mock object", () => {
* const mockObject = fn();
* expect(isMockObject(mockObject)).toBeTruthy();
* expect(isMockObject({})).toBeFalsy();
* });
* ```
*/
function isMockObject<A extends readonly unknown[] = any[], R = unknown>(
value: object,
): value is MockObject<A, R> {
const mock = prop("mock", value);
if (!isObject(mock)) return false;

return ["results", "calls", "callOrderNumbers"].every((key) =>
Array.isArray(prop(key, mock))
);
}

export { fn, isMockObject, MockFnStore };
export type { MockObject };
41 changes: 40 additions & 1 deletion mock/fn_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.

import { fn, MockFnStore } from "./fn.ts";
import { fn, isMockObject, MockFnStore } from "./fn.ts";
import { isFunction } from "../deps.ts";
import { expect } from "../expect/mod.ts";
import { assert, assertEquals, assertExists } from "../dev_deps.ts";
Expand Down Expand Up @@ -314,3 +314,42 @@ Deno.test("reset should clear once implementation and default", () => {
assertEquals(mockObject(), undefined);
assertEquals(mockObject(), undefined);
});

Deno.test("isMockObject", () => {
const table: [
...Parameters<typeof isMockObject>,
ReturnType<typeof isMockObject>,
][] = [
[{}, false],
[[], false],
[{ mock: {} }, false],
[{
mock: {
results: [],
callOrderNumbers: [],
},
}, false],
[{
mock: {
callOrderNumbers: [],
},
}, false],
[{
mock: {
calls: [],
results: [],
callOrderNumbers: [],
},
}, true],
[{
mock: {
calls: [[1]],
results: [{ type: "return", value: 1 }],
callOrderNumbers: [1],
},
}, true],
[fn(), true],
];

table.forEach(([value, result]) => assertEquals(isMockObject(value), result));
});

0 comments on commit 87c2ea4

Please sign in to comment.