Skip to content

Commit

Permalink
Fixes to handling of Map and Set objects in DID document props.
Browse files Browse the repository at this point in the history
- Map and Set objects are not treated correctly
- Added test cases with hierarchical objects
  • Loading branch information
shigeya authored and msporny committed May 24, 2021
1 parent b8983a5 commit 329cc57
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import matcher from '.';

expect.extend(matcher);


describe('.toBeDidDocumentMap', () => {
each([
[
{
"a": "1"
},
}
],
[
{
"b": [ "1", "2", "3" ]
},
{
],
[
{
"didDocumentMetadata": {
"canonicalId":"x",
"equivalentId":[
Expand All @@ -26,9 +31,11 @@ describe('.toBeDidDocumentMap', () => {
"published":true
}
}
}
},
],
]).test('passes when item is a type allowed in a DID Document map: %s', given => {
[new Map(Object.entries({ a: "1", b: "2", c: {"x": "1", "y": "2"}}))],
[new Set(["1", "2", "3", ["4", "5", "6"], {"x":"1", "y": "2"}])]
]).test('passes when item is a type allowed in a DID Document map: %s', given => {
expect(given).toBeDidDocumentMap();
});
});
Expand Down Expand Up @@ -62,7 +69,15 @@ describe('.not.toBeDidDocumentMap', () => {
"published":true
}
}}],
]).test('passes when not item is not of type DID Document map: %s', given => {
[new Map(Object.entries({ a: NaN, b: "2", c: {"x": "1", "y": "2"}}))],
[new Map(Object.entries({ a: "1", b: "2", c: {"x": NaN, "y": "2"}}))],
[new Map(Object.entries({ a: "1", b: undefined, c: {"x": "1", "y": "2"}}))],
[new Map(Object.entries({ a: "1", b: "2", c: {"x": "1", "y": undefined}}))],
[new Set(["1", "2", "3", ["4", NaN, "6"], {"x":"1", "y": "2"}])],
[new Set(["1", "2", "3", ["4", "5", undefined], {"x":"1", "y": "2"}])],
[new Set(["1", "2", "3", ["4", "5", "6"], {"x": undefined, "y": "2"}])],
[new Set(["1", "2", "3", ["4", "5", "6"], {"x": "1", "y": undefined }])]
]).test('passes when item is not of type DID Document map: %s', given => {
expect(given).not.toBeDidDocumentMap();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ export function predicate(expected) {
return expected.map(element => predicate(element)).reduce( (p, c) => (p && c), true);
}

if (expected instanceof Set) {
return expected.map(element => predicate(element)).reduce( (p, c) => (p && c), true);
if (expected instanceof Map || expected instanceof Set) {
let r = true;
expected.forEach(v => { r = r && predicate(v); });
return r;
}

if (expected instanceof Map) {
return expected.map(element => predicate(element)).reduce( (p, c) => (p && c), true);
}

if (typeof expected == "object") {
return Object.keys(expected).map(key => predicate(expected[key])).reduce( (p, c) => (p && c), true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ expect.extend(matcher);
describe('.toBeDidDocumentPropertyValueType', () => {
each([
["String"],
[new String],
[new String()],
[{}],
[new Map],
[{ a: "1", b: "2", c: {"x": "1", "y": "2"}}],
[new Map()],
[new Map(Object.entries({ a: "1", b: "2", c: {"x": "1", "y": "2"}}))],
[[]],
[new Array],
[new Set],
[new Array(1)],
[new Set()],
[new Set(["1", "2", "3", ["4", "5", "6"], {"x":"1", "y": "2"}])],
[true],
[false],
[null]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default expected => {
return true;
}

if (expected instanceof Set) {
if (expected instanceof Map || expected instanceof Set) {
return true;
}

Expand Down

0 comments on commit 329cc57

Please sign in to comment.