Skip to content

Commit

Permalink
feat(matcher): add toContainEntry matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Nov 27, 2021
1 parent a91338f commit e43e419
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ Released under the [MIT](./LICENSE) license
- toContainAllKeys
- toContainValues
- toContainAllValues
- toContainEntry
- toContainEntries
- toContainAllEntries
- toContainAnyEntries
Expand Down
1 change: 1 addition & 0 deletions matcher/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ export { toIncludeAllMembers } from "./to_include_all_members.ts";
export { toContainAnyKeys } from "./to_contain_any_keys.ts";
export { toContainValue } from "./to_contain_value.ts";
export { toContainAnyValues } from "./to_contain_any_values.ts";
export { toContainEntry } from "./to_contain_entry.ts";
2 changes: 2 additions & 0 deletions matcher/preset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.
// This module is browser compatible.
import { toContainEntry } from "./to_contain_entry.ts";
import { toContainAnyValues } from "./to_contain_any_values.ts";
import { toContainValue } from "./to_contain_value.ts";
import { toContainAnyKeys } from "./to_contain_any_keys.ts";
Expand Down Expand Up @@ -123,6 +124,7 @@ const jestMatcherMap = {
* @see https://github.com/jest-community/jest-extended
*/
const jestExtendedMatcherMap = {
toContainEntry,
toContainAnyValues,
toContainValue,
toContainAnyKeys,
Expand Down
29 changes: 29 additions & 0 deletions matcher/to_contain_entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.
// This module is browser compatible.

import { has, prop } from "./utils.ts";
import { equal } from "../deps.ts";
import type { MatchResult } from "./types.ts";

function predict(
actual: object,
[key, value]: [PropertyKey, unknown],
): boolean {
const _has = has(key, actual);
return _has && equal(prop(key, actual), value);
}

function toContainEntry(
actual: object,
expected: [PropertyKey, unknown],
): MatchResult {
return {
pass: predict(actual, expected),
actual: Object.entries(actual),
actualHint: "Actual entry:",
expectedHint: "Expected to contain:",
expected,
};
}

export { predict, toContainEntry };
39 changes: 39 additions & 0 deletions matcher/to_contain_entry_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.

import { assertEquals, assertFail, assertSuccess } from "../dev_deps.ts";
import { predict, toContainEntry } from "./to_contain_entry.ts";

Deno.test({
name: "predict",
fn: () => {
const table: [...Parameters<typeof predict>, ReturnType<typeof predict>][] =
[
[{}, ["", {}], false],
[{ a: 1 }, ["a", 1], true],
[{ a: 1, b: 2 }, ["a", 2], false],
[{ a: 1, b: 2 }, [1, 2], false],
[{ a: 1, [Symbol.for("test")]: 2 }, [Symbol.for("test"), 2], true],
[{ a: {}, b: [] }, ["b", []], true],
];

table.forEach(([actual, expected, result]) =>
assertEquals(predict(actual, expected), result)
);
},
});

Deno.test({
name: "toContainEntry",
fn: () => {
assertSuccess(toContainEntry({ "": {} }, ["", {}]));
assertFail(toContainEntry({}, ["", ""]));

assertEquals(toContainEntry({ a: 1, b: 2, c: 3 }, ["abc", "def"]), {
pass: false,
expected: ["abc", "def"],
expectedHint: "Expected to contain:",
actual: [["a", 1], ["b", 2], ["c", 3]],
actualHint: "Actual entry:",
});
},
});
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export { toIncludeAllMembers } from "./matcher/to_include_all_members.ts";
export { toContainAnyKeys } from "./matcher/to_contain_any_keys.ts";
export { toContainValue } from "./matcher/to_contain_value.ts";
export { toContainAnyValues } from "./matcher/to_contain_any_values.ts";
export { toContainEntry } from "./matcher/to_contain_entry.ts";
export { toBeHexColor } from "./matcher/to_be_hex_color.ts";
export { toBeDateString } from "./matcher/to_be_date_string.ts";
export { toBeSealed } from "./matcher/to_be_sealed.ts";
Expand Down

0 comments on commit e43e419

Please sign in to comment.