ts-mockito#deepEqual TypeScript Examples
The following examples show how to use
ts-mockito#deepEqual.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: userStorage.spec.ts From space-sdk with MIT License | 4 votes |
describe('UserStorage', () => {
describe('createFolder()', () => {
it('should throw error if user is not authenticated', async () => {
const storage = new UserStorage({ identity: mockIdentity, token: '', endpoint: '' });
await expect(storage.createFolder({ bucket: '', path: '' })).to.eventually.be.rejectedWith(UnauthenticatedError);
});
it('should push empty .keep file to bucket at specified path', async () => {
const createFolderRequest = { bucket: 'personal', path: 'topLevel' };
const { storage, mockBuckets } = initStubbedStorage();
await storage.createFolder(createFolderRequest);
verify(
mockBuckets.pushPath(
'myBucketKey',
'.keep',
deepEqual({
path: '/topLevel/.keep',
content: Buffer.from(''),
}),
),
).called();
});
});
describe('listDirectory()', () => {
it('should throw error if user is not authenticated', async () => {
const storage = new UserStorage({ identity: mockIdentity, token: '', endpoint: '' });
await expect(storage.listDirectory({ bucket: 'bucket', path: '' })).to.eventually.be.rejectedWith(
UnauthenticatedError,
);
});
it('should return list of items', async () => {
const listDirectoryRequest = { bucket: 'personal', path: 'topLevel' };
const mainItem = mock<PathItem>();
const childItem = {
name: 'folder',
path: '/ipfs/Qm123/folder',
cid: 'Qm...',
isDir: true,
size: 10,
};
const updatedAt = (new Date().getMilliseconds()) * 1000000;
const { storage, mockBuckets } = initStubbedStorage();
when(mockBuckets.listPath('myBucketKey', `/${listDirectoryRequest.path}`, 0)).thenResolve({
item: {
...mainItem,
items: [
{
...childItem,
metadata: {
updatedAt,
roles: new Map(),
},
items: [],
count: 1,
},
],
},
});
const mockMembers = new Map<string, PathAccessRole>();
const pubkey = 'bbaareieswor4fnmzdwmv6fwij2rxyyjmpc2izognkiqnfxlvnzzsvs7y5y';
mockMembers.set(pubkey, PathAccessRole.PATH_ACCESS_ROLE_WRITER);
when(mockBuckets.pullPathAccessRoles(anyString(), anyString())).thenResolve(mockMembers);
const result = await storage.listDirectory(listDirectoryRequest);
const expectedDate = dayjs(new Date(Math.round(updatedAt / 1000000))).format();
expect(result).to.not.equal(undefined);
expect(result.items[0]).to.not.equal(undefined);
expect(result.items[0].name).to.equal(childItem.name);
expect(result.items[0].bucket).to.not.be.empty;
expect(result.items[0].dbId).to.not.be.empty;
expect(result.items[0].ipfsHash).to.equal(childItem.cid);
expect(result.items[0].isDir).to.equal(childItem.isDir);
expect(result.items[0].sizeInBytes).to.equal(childItem.size);
expect(result.items[0].created).to.equal(expectedDate);
expect(result.items[0].updated).to.equal(expectedDate);
expect(result.items[0].fileExtension).to.equal('');
expect(result.items[0].isLocallyAvailable).to.equal(false);
expect(result.items[0].backupCount).to.equal(1);
expect(result.items[0].members).to.deep.equal([{
publicKey: Buffer.from(tryParsePublicKey(pubkey).pubKey).toString('hex'),
role: PathAccessRole.PATH_ACCESS_ROLE_WRITER,
address: GetAddressFromPublicKey(pubkey),
}]);
expect(result.items[0].isBackupInProgress).to.equal(false);
expect(result.items[0].isRestoreInProgress).to.equal(false);
expect(result.items[0].uuid).to.equal('generated-uuid');
});
});
describe('openFile()', () => {
// it('should throw error if user is not authenticated', async () => {
// const storage = new UserStorage({ identity: mockIdentity, token: '' });
// await expect(storage.openFile({ bucket: 'bucket', path: '' })).to.eventually.be.rejectedWith(
// UnauthenticatedError,
// );
// });
it('should throw if file is not found', async () => {
const { storage, mockBuckets } = initStubbedStorage();
when(mockBuckets.pullPath('myBucketKey', '/file.txt', anything())).thenThrow(
new Error('Error: no link named "file.txt" under QmVQWu2C3ZgdoAmBsffFASrgynAfgvYX8CCK4o9SxRvC4p'),
);
await expect(storage.openFile({ bucket: 'personal', path: '/file.txt' })).to.eventually.be.rejectedWith(
DirEntryNotFoundError,
);
});
it('should return a valid stream of files data', async () => {
const { storage, mockBuckets } = initStubbedStorage();
const actualFileContent = "file.txt's file content";
when(mockBuckets.pullPath('myBucketKey', '/file.txt', anything())).thenReturn(
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
newEncryptedDataWriter(actualFileContent, decodeFileEncryptionKey(encryptionKey)),
);
const result = await storage.openFile({ bucket: 'personal', path: '/file.txt' });
const filesData = await result.consumeStream();
expect(new TextDecoder('utf8').decode(filesData)).to.equal(actualFileContent);
expect(result.mimeType).to.equal('generic/type');
});
});
describe('openFileByUuid', () => {
// it('should throw if uuid is not found', async () => {
// // fix this when mocking metadatastore works
// });
it('should return a valid stream of files data', async () => {
const { storage, mockBuckets } = initStubbedStorage();
const fileUuid = v4();
const actualFileContent = "file.txt's file content";
when(mockBuckets.existing('mockThreadId')).thenReturn(Promise.resolve([
{
...mock<Root>(),
name: 'myBucket',
key: 'myBucketKey',
},
]));
when(mockBuckets.listPath('myBucketKey', anyString())).thenResolve({
item: {
...mock<PathItem>(),
name: 'file.txt',
path: '/ipfs/Qm123/file.txt',
metadata: {
updatedAt: (new Date().getMilliseconds()) * 1000000,
roles: new Map(),
},
items: [],
},
});
when(mockBuckets.pullPath('myBucketKey', anyString(), anything())).thenReturn(
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
newEncryptedDataWriter(actualFileContent, decodeFileEncryptionKey(encryptionKey)),
);
const mockMembers = new Map<string, PathAccessRole>();
mockMembers.set('dummykey', PathAccessRole.PATH_ACCESS_ROLE_WRITER);
when(mockBuckets.pullPathAccessRoles('myBucketKey', '/ipfs/Qm123/file.txt')).thenResolve(mockMembers);
const result = await storage.openFileByUuid({ uuid: fileUuid });
const filesData = await result.consumeStream();
expect(new TextDecoder('utf8').decode(filesData)).to.equal(actualFileContent);
expect(result.mimeType).to.equal('generic/type');
expect(result.entry.bucket).to.not.be.empty;
expect(result.entry.dbId).to.not.be.empty;
});
});
describe('addItems()', () => {
it('should publish data, error and done events correctly', async () => {
const { storage, mockBuckets } = initStubbedStorage();
const uploadError = new Error('update is non-fast-forward');
when(mockBuckets.pushPath('myBucketKey', anyString(), anything(), anything())).thenResolve({
...mock<PushPathResult>(),
});
const childItem = {
name: 'entryName',
path: '/ipfs/Qm123/entryName',
cid: 'Qm...',
isDir: false,
size: 10,
};
when(mockBuckets.listPath('myBucketKey', anyString())).thenResolve({
item: {
...mock<PathItem>(),
name: 'entryName',
path: '/ipfs/Qm123/entryName',
cid: 'Qm...',
isDir: false,
size: 10,
metadata: {
updatedAt: (new Date().getMilliseconds()) * 1000000,
roles: new Map<string, PathAccessRole>(),
},
count: 0,
items: [],
},
});
// fail upload of b.txt
when(mockBuckets.pushPath('myBucketKey', '/b.txt', anything(), anything())).thenReject(uploadError);
const callbackData = {
data: [] as AddItemsEventData[],
error: [] as AddItemsEventData[],
done: [] as AddItemsEventData[],
};
// upload files
const uploadResponse = await storage.addItems({
bucket: 'personal',
files: [
{
path: '/top/a.txt',
data: 'a content',
mimeType: 'text/plain',
},
{
path: 'b.txt',
data: 'b content',
mimeType: 'text/plain',
},
],
});
// listen for status events
uploadResponse.on('data', (it) => callbackData.data.push(it));
uploadResponse.on('error', (err) => callbackData.error.push(err));
await new Promise((resolve) => {
uploadResponse.once('done', (it) => {
callbackData.done.push(it);
resolve();
});
});
// verify callback data
expect(callbackData.data).to.containSubset([
{ path: '/top/a.txt', status: 'success' },
{ path: '/top', status: 'success' },
]);
expect(callbackData.error).to.containSubset([{ path: '/b.txt', status: 'error', error: uploadError }]);
expect(callbackData.done).to.containSubset([
{
bucket: 'personal',
files: [
{ path: '/top', status: 'success' },
{ path: '/top/a.txt', status: 'success' },
{ path: '/b.txt', status: 'error', error: uploadError },
],
},
]);
});
});
describe('shareViaPublicKey()', () => {
let storage: UserStorage;
let mockBuckets: Buckets;
beforeEach(() => {
const stub = initStubbedStorage();
storage = stub.storage;
mockBuckets = stub.mockBuckets;
});
it('should throw if public keys are empty', async () => {
await expect(
storage.shareViaPublicKey({
publicKeys: [],
paths: [
{
bucket: 'personal',
path: '/randomPath',
uuid: v4(),
},
],
}),
).to.eventually.be.rejected;
});
it('should throw if public keys are not valid', async () => {
await expect(
storage.shareViaPublicKey({
publicKeys: [{
id: '[email protected]',
pk: 'invalid-pk-provided',
}],
paths: [
{
bucket: 'personal',
path: '/randomPath',
uuid: v4(),
},
],
}),
).to.eventually.be.rejectedWith('Unsupported encoding: i');
});
});
});
Example #2
Source File: feature-toggle.guard.spec.ts From canopy with Apache License 2.0 | 4 votes |
describe('FeatureToggleGuard', () => {
let configServiceMock: LgFeatureToggleService;
let guard: FeatureToggleGuard;
const routerMock = mock(Router);
const enabledConfig = { parent: true };
const disabledConfig = { parent: false };
const enabledConfig2 = { parent: true, child: true };
const disabledConfig2 = { parent: false, child: false };
const routeSnapshot: Partial<ActivatedRouteSnapshot> = {
data: { featureToggle: 'parent' },
children: [
{
data: { featureToggle: 'child' },
} as any,
],
};
const checkGuardConfigs = (
guardType: GuardTypes,
config,
marbleValue: boolean,
onceVerify: boolean = false,
) => {
/* eslint-disable @typescript-eslint/no-unused-expressions, no-unused-expressions */
onceVerify
? when(configServiceMock.toggles$).thenReturn(config)
: when(configServiceMock.toggles$).thenReturn(of(config));
/* eslint-enable */
switch (guardType) {
case GuardTypes.CAN_ACTIVATE:
expect(guard.canActivate(routeSnapshot as ActivatedRouteSnapshot)).toBeObservable(
cold('(a|)', { a: marbleValue }),
);
break;
case GuardTypes.CAN_ACTIVATE_CHILD:
expect(
guard.canActivateChild(routeSnapshot as ActivatedRouteSnapshot),
).toBeObservable(cold('(a|)', { a: marbleValue }));
break;
case GuardTypes.CAN_LOAD:
expect(guard.canLoad(routeSnapshot as Route)).toBeObservable(
cold('(a|)', { a: marbleValue }),
);
break;
}
return verify(
routerMock.navigate(deepEqual([ '/' ]), deepEqual({ queryParamsHandling: 'merge' })),
);
};
beforeEach(() => {
configServiceMock = mock(LgFeatureToggleService);
TestBed.configureTestingModule({
providers: [
FeatureToggleGuard,
{
provide: LgFeatureToggleService,
useFactory: () => instance(configServiceMock),
},
{ provide: Router, useFactory: () => instance(routerMock) },
],
});
guard = TestBed.inject(FeatureToggleGuard);
});
describe('can activate', () => {
it('should be true when the parent is enabled', () => {
checkGuardConfigs(GuardTypes.CAN_ACTIVATE, enabledConfig, true).never();
reset(routerMock);
});
it('should be true when the parent and child is enabled', () => {
checkGuardConfigs(GuardTypes.CAN_ACTIVATE, enabledConfig2, true).never();
reset(routerMock);
});
it('should be false when the parent is disabled', () => {
checkGuardConfigs(GuardTypes.CAN_ACTIVATE_CHILD, disabledConfig, false).once();
reset(routerMock);
});
it('should be false when the parent is enabled but not child', () => {
checkGuardConfigs(GuardTypes.CAN_ACTIVATE, disabledConfig2, false).once();
reset(routerMock);
});
});
describe('can activate child', () => {
it('should be true when the parent is enabled', () => {
checkGuardConfigs(GuardTypes.CAN_ACTIVATE_CHILD, of(enabledConfig), true).never();
reset(routerMock);
});
it('should be true when the parent and child is enabled', () => {
checkGuardConfigs(GuardTypes.CAN_ACTIVATE_CHILD, of(enabledConfig2), true).never();
reset(routerMock);
});
it('should be false when the parent is disabled', () => {
checkGuardConfigs(
GuardTypes.CAN_ACTIVATE_CHILD,
of(disabledConfig),
false,
true,
).once();
reset(routerMock);
});
it('should be false when the parent is enabled but not child', () => {
checkGuardConfigs(
GuardTypes.CAN_ACTIVATE_CHILD,
of(disabledConfig2),
false,
true,
).once();
reset(routerMock);
});
});
describe('can load', () => {
it('should be true when the parent is enabled', () => {
checkGuardConfigs(GuardTypes.CAN_LOAD, of(enabledConfig), true).never();
reset(routerMock);
});
it('should be true when the parent and child is enabled', () => {
checkGuardConfigs(GuardTypes.CAN_LOAD, of(enabledConfig2), true).never();
reset(routerMock);
});
it('should be false when the parent is disabled', () => {
checkGuardConfigs(GuardTypes.CAN_LOAD, of(disabledConfig), false, true).once();
reset(routerMock);
});
it('should be false when the parent is enabled but not child', () => {
checkGuardConfigs(GuardTypes.CAN_LOAD, of(disabledConfig2), false, true).once();
reset(routerMock);
});
});
});