Skip to content

Commit

Permalink
feat(matcher): add toContainEntries matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Nov 27, 2021
1 parent 8005015 commit beb20b2
Show file tree
Hide file tree
Showing 6 changed files with 55 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
- toContainEntries
- toContainAllEntries
- ~~toResolve~~ not pure
- ~~toReject~~ not pure
Expand Down
1 change: 1 addition & 0 deletions matcher/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ export { toContainValue } from "./to_contain_value.ts";
export { toContainAnyValues } from "./to_contain_any_values.ts";
export { toContainEntry } from "./to_contain_entry.ts";
export { toContainAnyEntries } from "./to_contain_any_entries.ts";
export { toContainEntries } from "./to_contain_entries.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 { toContainEntries } from "./to_contain_entries.ts";
import { toContainAnyEntries } from "./to_contain_any_entries.ts";
import { toContainEntry } from "./to_contain_entry.ts";
import { toContainAnyValues } from "./to_contain_any_values.ts";
Expand Down Expand Up @@ -125,6 +126,7 @@ const jestMatcherMap = {
* @see https://github.com/jest-community/jest-extended
*/
const jestExtendedMatcherMap = {
toContainEntries,
toContainAnyEntries,
toContainEntry,
toContainAnyValues,
Expand Down
22 changes: 22 additions & 0 deletions matcher/to_contain_entries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.
// This module is browser compatible.

import { containAll } from "./utils.ts";
import type { MatchResult } from "./types.ts";

function toContainEntries(
actual: object,
expected: [string, unknown][],
): MatchResult {
const entries = Object.entries(actual);

return {
pass: containAll(entries, expected),
actual: entries,
actualHint: "Actual entries:",
expected,
expectedHint: "Expected to contain all:",
};
}

export { toContainEntries };
29 changes: 29 additions & 0 deletions matcher/to_contain_entries_test.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.

import { assertEquals, assertFail, assertSuccess } from "../dev_deps.ts";
import { toContainEntries } from "./to_contain_entries.ts";

Deno.test({
name: "toContainEntries",
fn: () => {
assertSuccess(toContainEntries({}, []));
assertSuccess(toContainEntries({ a: 1 }, [["a", 1]]));
assertSuccess(
toContainEntries({ a: 1, b: "", c: {} }, [["c", {}], ["a", 1]]),
);
assertFail(
toContainEntries({ a: 1, b: "", c: {} }, [["c", {}], ["a", 1], [
"d",
null,
]]),
);

assertEquals(toContainEntries({ a: 1 }, [["a", 1], ["b", 2], ["c", "3"]]), {
pass: false,
actual: [["a", 1]],
actualHint: "Actual entries:",
expected: [["a", 1], ["b", 2], ["c", "3"]],
expectedHint: "Expected to contain all:",
});
},
});
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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 { toContainAnyEntries } from "./matcher/to_contain_any_entries.ts";
export { toContainEntries } from "./matcher/to_contain_entries.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 beb20b2

Please sign in to comment.