Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit Tests] Update unit tests #256

Merged
merged 9 commits into from
Jan 14, 2025
2 changes: 1 addition & 1 deletion src/persistence/MCWSPersistenceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class MCWSPersistenceProvider extends BaseMCWSPersistenceProvider
} catch (error) {
console.warn('MCWSPersistneceProvider:update', error);

return;
return false;
}
}

Expand Down
340 changes: 224 additions & 116 deletions src/persistence/test/MCWSNamespaceModelProviderSpec.js
Original file line number Diff line number Diff line change
@@ -1,138 +1,246 @@
/*global define,describe,beforeEach,jasmine,Promise,it,expect*/
define([
// '../src/MCWSNamespaceModelProvider'
], function (
// MCWSNamespaceModelProvider
) {
'use strict';

xdescribe('MCWSNamespaceModelProvider', function () {
var namespaceService,

import MCWSUserContainerProvider from '../MCWSUserContainerProvider';
import MCWSPersistenceProvider from '../MCWSPersistenceProvider';
import mcws from 'services/mcws/mcws';

describe('MCWS Providers', () => {
let openmct;
let someNamespace;
let anotherNamespace;
let personalContainerNamespace;
let personalNamespace;
let namespaces;
let userContainerProvider;
let persistenceProvider;

beforeEach(() => {
openmct = {
user: {
getCurrentUser: () => Promise.resolve({ id: 'myUser' })
}
};

someNamespace = {
id: 'some-namespace:root',
key: 'some-namespace',
name: 'Some Namespace',
url: '/some/namespace/url'
};
anotherNamespace = {
id: 'another-namespace:root',
key: 'another-namespace',
name: 'Another Namespace',
url: '/another/namespace/url',
location: 'some-namespace:root'
};
personalContainerNamespace = {
id: 'personal',
key: 'personal',
name: 'personal',
url: '/some/personal/namespace',
containsNamespaces: true,
childTemplate: {
id: 'personal-{username}:root',
key: 'personal-{username}',
name: '{username}',
url: '/some/personal/namespace/{username}'
}
};
personalNamespace = {
id: 'personal-myUser:root',
key: 'personal-myUser',
name: 'myUser',
url: '/some/personal/namespace/myUser',
location: 'personal'
};

namespaces = [
someNamespace,
anotherNamespace,
personalContainerNamespace,
personalNamespace,
namespaces,
provider;

beforeEach(function () {
namespaceService = jasmine.createSpyObj(
'namespaceService',
[
'getPersistenceNamespaces',
'getContainedNamespaces'
]
personalNamespace
];

const roots = [personalContainerNamespace];
userContainerProvider = new MCWSUserContainerProvider(openmct, roots);
persistenceProvider = new MCWSPersistenceProvider(openmct, roots);

// Mock mcws service calls
spyOn(userContainerProvider, 'getPersistenceNamespaces')
.and.returnValue(Promise.resolve(namespaces));
spyOn(userContainerProvider, 'getContainedNamespaces')
.and.callFake((namespace) => {
if (namespace.id === 'personal') {
return Promise.resolve([personalNamespace]);
}
return Promise.resolve([]);
});
});

describe('MCWSUserContainerProvider', () => {
it('gets container model with contained namespaces', async () => {
const identifier = {
namespace: 'personal',
key: 'container'
};
const model = await userContainerProvider.get(identifier);

expect(model.type).toBe('folder');
expect(model.composition).toEqual([{ key: 'root', namespace: 'personal-myUser' }]);
expect(model.location).toBe('ROOT');
});
});

describe('MCWSPersistenceProvider', () => {
let mcwsNamespace;

beforeEach(() => {
// Mock mcws namespace operations
const fileOps = {
read: () => Promise.resolve({
json: () => Promise.resolve({
type: 'folder',
name: 'Test Object'
})
}),
create: jasmine.createSpy('create').and.returnValue(Promise.resolve(true)),
replace: jasmine.createSpy('replace').and.returnValue(Promise.resolve(true))
};
mcwsNamespace = {
opaqueFile: () => fileOps
};

spyOn(persistenceProvider, 'getPersistenceNamespaces')
.and.returnValue(Promise.resolve(namespaces));
// Mock the private getNamespace method through the mcws import
spyOn(mcws, 'namespace').and.returnValue(mcwsNamespace);
});

it('gets persisted objects', async () => {
const identifier = {
namespace: 'personal-myUser',
key: 'some-object'
};
const result = await persistenceProvider.get(identifier);

expect(result).toBeDefined();
expect(result.type).toBe('folder');
expect(result.name).toBe('Test Object');
expect(result.identifier).toEqual(identifier);
});

it('handles abort signal when getting objects', async () => {
const identifier = {
namespace: 'personal-myUser',
key: 'some-object'
};
const abortSignal = new AbortController().signal;

await persistenceProvider.get(identifier, abortSignal);

expect(mcws.namespace).toHaveBeenCalledWith(
jasmine.any(String),
{ signal: abortSignal }
);
});

someNamespace = {
id: 'some-namespace:root',
key: 'some-namespace',
name: 'Some Namespace',
url: '/some/namespace/url'
it('creates new objects', async () => {
const domainObject = {
identifier: {
namespace: 'some-namespace',
key: 'new-object'
},
type: 'folder',
name: 'New Folder'
};
anotherNamespace = {
id: 'another-namespace:root',
key: 'another-namespace',
name: 'Another Namespace',
url: '/another/namespace/url',
location: 'some-namespace:root'

const success = await persistenceProvider.create(domainObject);
const expectedModel = {
type: 'folder',
name: 'New Folder'
};
personalContainerNamespace = {
id: 'personal',
key: 'personal',
name: 'personal',
url: '/some/personal/namespace'

expect(success).toBe(true);
expect(mcwsNamespace.opaqueFile('new-object').create)
.toHaveBeenCalledWith(expectedModel);
});

it('updates existing objects', async () => {
const domainObject = {
identifier: {
namespace: 'some-namespace',
key: 'existing-object'
},
type: 'folder',
name: 'Updated Folder'
};
personalNamespace = {
id: 'personal-myUser:root',
key: 'personal-myUser}',
name: 'myUser',
url: '/some/personal/namespace/myUser',
location: 'personal'

const success = await persistenceProvider.update(domainObject);
const expectedModel = {
type: 'folder',
name: 'Updated Folder'
};

namespaces = [
someNamespace,
anotherNamespace,
personalContainerNamespace,
personalNamespace
];
expect(success).toBe(true);
expect(mcwsNamespace.opaqueFile('existing-object').replace)
.toHaveBeenCalledWith(expectedModel);
});

namespaceService
.getPersistenceNamespaces
.and.returnValue(Promise.resolve(namespaces));
it('handles errors during get operation', async () => {
const errorNamespace = {
opaqueFile: () => ({
read: () => Promise.reject(new Error('Network Error'))
})
};
mcws.namespace.and.returnValue(errorNamespace);

namespaceService
.getContainedNamespaces
.and.callFake(function (namespace) {
if (namespace.id === 'personal') {
return Promise.resolve([personalNamespace]);
}
return Promise.resolve([]);
});
const identifier = {
namespace: 'personal-myUser',
key: 'error-object'
};
const result = await persistenceProvider.get(identifier);

provider = new MCWSNamespaceModelProvider(namespaceService);
expect(result).toBeUndefined();
});

describe('getModels', function () {
var someNamespaceModel,
anotherNamespaceModel,
personalContainerNamespaceModel,
personalNamespaceModel,
allNamespaceModels;

beforeEach(function (done) {
provider
.getModels([
'some-namespace:root',
'another-namespace:root',
'personal',
'personal-myUser:root'
])
.then(function (models) {
someNamespaceModel = models['some-namespace:root'];
anotherNamespaceModel =
models['another-namespace:root'];
personalContainerNamespaceModel = models.personal;
personalNamespaceModel = models['personal-myUser:root'];
allNamespaceModels = [
someNamespaceModel,
anotherNamespaceModel,
personalContainerNamespaceModel,
personalNamespaceModel
];
})
.then(done);
});
it('handles errors during create operation', async () => {
const errorNamespace = {
opaqueFile: () => ({
create: () => Promise.reject(new Error('Creation Error'))
})
};
mcws.namespace.and.returnValue(errorNamespace);

it('sets type to folder', function () {
allNamespaceModels.forEach(function (namespaceModel) {
expect(namespaceModel.type).toBe('folder');
});
});
const domainObject = {
identifier: {
namespace: 'personal-myUser',
key: 'error-object'
},
type: 'folder'
};
const success = await persistenceProvider.create(domainObject);

it('uses location specified in namespace definition', function () {
expect(anotherNamespaceModel.location)
.toBe('some-namespace:root');
expect(personalNamespaceModel.location)
.toBe('personal');
});
expect(success).toBe(false);
});

it('sets default location if not specified', function () {
expect(someNamespaceModel.location).toBe('ROOT');
expect(personalContainerNamespaceModel.location).toBe('ROOT');
});
it('handles errors during update operation', async () => {
const errorNamespace = {
opaqueFile: () => ({
replace: () => Promise.reject(new Error('Update Error'))
})
};
mcws.namespace.and.returnValue(errorNamespace);

it('sets composition', function () {
expect(someNamespaceModel.composition)
.toEqual(jasmine.any(Array));
expect(anotherNamespaceModel.composition)
.toEqual(jasmine.any(Array));
expect(personalContainerNamespaceModel.composition)
.toEqual(['personal-myUser:root']);
expect(personalNamespaceModel.composition)
.toEqual(jasmine.any(Array));
});
const domainObject = {
identifier: {
namespace: 'personal-myUser',
key: 'error-object'
},
type: 'folder'
};
const success = await persistenceProvider.update(domainObject);

expect(success).toBe(false);
});
});
});
Loading
Loading