@microsoft/useragent-sdk
Version:
SDK for building decentralized identity wallets and enterprise agents.
82 lines • 3.6 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
const TestUtils_1 = require("./TestUtils");
const Commit_1 = require("../../src/hubSession/Commit");
const src_1 = require("../../src");
const HubInterface_1 = require("../../src/hubInterfaces/HubInterface");
const commitFields = {
interface: src_1.HubInterfaceType.Collections,
context: 'schema.org',
type: 'MusicPlaylist',
operation: HubInterface_1.Operation.Create,
committed_at: '2019-01-01',
commit_strategy: src_1.CommitStrategyType.Basic,
iss: 'did:example:sub.id',
sub: 'did:example:sub.id',
payload: {
title: 'My Playlist',
},
object_id: undefined
};
describe('Commit', () => {
let commit;
beforeAll(() => {
commit = new Commit_1.default(commitFields);
});
describe('validate', () => {
const invalidStrings = ['', null, undefined, true, false, 7, [], [''], {}];
const invalidCases = {
//'interface': invalidStrings,
'context': invalidStrings,
'type': invalidStrings,
'committed_at': invalidStrings,
//'commit_strategy': invalidStrings,
'sub': invalidStrings,
'operation': [...invalidStrings, 'other'],
'payload': [true, false, null, undefined, 77],
};
Object.keys(invalidCases).forEach((field) => {
const cases = invalidCases[field];
cases.forEach(invalidValue => {
it(`should reject '${field}' set to ${TestUtils_1.explain(invalidValue)}`, async () => {
const alteredFields = TestUtils_1.alter(commitFields, { [field]: invalidValue });
const commit = new Commit_1.default(alteredFields);
const result = commit.isValid();
expect(result).toBeFalsy();
});
});
});
it('should ensure object_id is not set for a create commit', async () => {
const alteredFields = TestUtils_1.alter(commitFields, {
'operation': 'create',
'object_id': 'abc'
});
const commit = new Commit_1.default(alteredFields);
expect(commit.isValid()).toBeFalsy();
});
it('should ensure object_id is set for an update/delete commit', async () => {
['update', 'delete'].forEach((operation) => {
const alteredFields = TestUtils_1.alter(commitFields, {
'operation': operation,
'object_id': undefined
});
const commit = new Commit_1.default(alteredFields);
expect(commit.isValid()).toBeFalsy();
});
});
it('should validate a correct commit', async () => {
const commit = new Commit_1.default(commitFields);
expect(commit.isValid()).toBeTruthy();
});
});
describe('getPayload()', () => {
it('should return the payload', async () => {
expect(commit.getPayload()).toEqual(commitFields.payload);
});
});
});
//# sourceMappingURL=Commit.spec.js.map