mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-08 12:37:00 +03:00
add tests, new partialDeepEquals
This commit is contained in:
134
test/cases/testUtils.ts
Normal file
134
test/cases/testUtils.ts
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
import assert from "assert";
|
||||||
|
|
||||||
|
import { partialDeepEquals } from "../utils";
|
||||||
|
|
||||||
|
describe("Test utils ", () => {
|
||||||
|
it("objectContain", async () => {
|
||||||
|
assert(partialDeepEquals(
|
||||||
|
{
|
||||||
|
name: "John Wick",
|
||||||
|
old: 20,
|
||||||
|
vip: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
), "Did not match empty expect");
|
||||||
|
assert(partialDeepEquals(
|
||||||
|
[{a: [1,2,3]}, {a: [1,2]}],
|
||||||
|
[{a: [1,2,3]}, {a: [1,2]}]
|
||||||
|
), "Did not match same arrays");
|
||||||
|
assert(partialDeepEquals(
|
||||||
|
{
|
||||||
|
name: "John Wick",
|
||||||
|
old: 20,
|
||||||
|
vip: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
vip: true,
|
||||||
|
old: 20
|
||||||
|
}
|
||||||
|
), "Did not match same partial properties");
|
||||||
|
// do not match incorrect propeties
|
||||||
|
assert(!partialDeepEquals(
|
||||||
|
{
|
||||||
|
name: "John Wick",
|
||||||
|
old: 20,
|
||||||
|
vip: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
vip: false,
|
||||||
|
old: 19
|
||||||
|
}
|
||||||
|
), "Matched different properties");
|
||||||
|
// do not match missing property
|
||||||
|
assert(!partialDeepEquals(
|
||||||
|
{
|
||||||
|
name: "John Wick",
|
||||||
|
old: 20,
|
||||||
|
vip: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
vip: true,
|
||||||
|
child: {
|
||||||
|
name: ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
));
|
||||||
|
assert(!partialDeepEquals(
|
||||||
|
{
|
||||||
|
name: "John Wick",
|
||||||
|
old: 20,
|
||||||
|
vip: true,
|
||||||
|
child: {
|
||||||
|
name: "a"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
vip: true,
|
||||||
|
child: {
|
||||||
|
name: ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
), "Matched incorrect child property");
|
||||||
|
assert(!partialDeepEquals(
|
||||||
|
{
|
||||||
|
name: "John Wick",
|
||||||
|
old: 20,
|
||||||
|
vip: true,
|
||||||
|
child: {
|
||||||
|
name: "a",
|
||||||
|
child: {
|
||||||
|
name: "a",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
vip: true,
|
||||||
|
child: {
|
||||||
|
name: "a",
|
||||||
|
child: {
|
||||||
|
name: "b",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
), "Matched incorrected 2-nested property");
|
||||||
|
assert(partialDeepEquals(
|
||||||
|
{
|
||||||
|
name: "John Wick",
|
||||||
|
old: 20,
|
||||||
|
vip: true,
|
||||||
|
child: {
|
||||||
|
name: "a",
|
||||||
|
child: {
|
||||||
|
name: "b",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
vip: true,
|
||||||
|
child: {
|
||||||
|
name: "a",
|
||||||
|
child: {
|
||||||
|
name: "b",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
), "Did not match exact child properties");
|
||||||
|
assert(partialDeepEquals(
|
||||||
|
{
|
||||||
|
name: "John Wick",
|
||||||
|
values: [{
|
||||||
|
name: "a",
|
||||||
|
}, {
|
||||||
|
name: "b",
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
values: [{
|
||||||
|
name: "a",
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
), "Did not match partial child array");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -8,3 +8,19 @@ export function getbaseURL(): string {
|
|||||||
* Duplicated from Mocha types. TypeScript doesn't infer that type by itself for some reason.
|
* Duplicated from Mocha types. TypeScript doesn't infer that type by itself for some reason.
|
||||||
*/
|
*/
|
||||||
export type Done = (err?: any) => void;
|
export type Done = (err?: any) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Check object contains expected properties
|
||||||
|
*/
|
||||||
|
export const partialDeepEquals = (actual: Record<string, any>, expected: Record<string, any>): boolean => {
|
||||||
|
// loop over key, value of expected
|
||||||
|
for (const [ key, value ] of Object.entries(expected)) {
|
||||||
|
// if value is object or array, recurse
|
||||||
|
if (Array.isArray(value) || typeof value === "object") {
|
||||||
|
if (!partialDeepEquals(actual?.[key], value)) return false;
|
||||||
|
}
|
||||||
|
else if (actual?.[key] !== value) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user