diff --git a/src/persistence/test/MCWSNamespaceModelProviderSpec.js b/src/persistence/test/MCWSNamespaceModelProviderSpec.js index 4b6640a..761bcdd 100644 --- a/src/persistence/test/MCWSNamespaceModelProviderSpec.js +++ b/src/persistence/test/MCWSNamespaceModelProviderSpec.js @@ -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); }); }); }); diff --git a/src/persistence/test/MCWSNamespaceServiceSpec.js b/src/persistence/test/MCWSNamespaceServiceSpec.js index dae5794..ec210f9 100644 --- a/src/persistence/test/MCWSNamespaceServiceSpec.js +++ b/src/persistence/test/MCWSNamespaceServiceSpec.js @@ -1,382 +1,358 @@ -/*global define,describe,beforeEach,jasmine,Promise,it,expect*/ - -define([ - // '../src/MCWSNamespaceService', - // 'services/mcws/mcws' -], function ( - // MCWSNamespaceService, - // mcwsDefault -) { - 'use strict'; - - // const mcws = mcwsDefault.default; - - xdescribe('MCWSNamespaceService', function () { - var $window, - namespaceMIOs, - mockUserAPI, - mockOpenmct, - sharedRootDefinition, - inaccessibleSharedRootDefinition, - missingSharedRootDefinition, - sharedRootDefinitions, - containerRootDefinition, - missingContainerRootDefinition, - inaccessibleContainerRootDefinition, - containerRootDefinitions, - namespaceService; - - beforeEach(function () { - $window = { - location: { - pathname: '/mcws/clients/vista/' - } - }; +import MCWSPersistenceProvider from '../MCWSPersistenceProvider'; +import MCWSUserContainerProvider from '../MCWSUserContainerProvider'; +import mcws from '../../services/mcws/mcws'; + +describe('MCWSNamespaceService', () => { + let namespaceMIOs; + let mockUserAPI; + let mockOpenmct; + let sharedRootDefinition; + let inaccessibleSharedRootDefinition; + let missingSharedRootDefinition; + let sharedRootDefinitions; + let containerRootDefinition; + let missingContainerRootDefinition; + let inaccessibleContainerRootDefinition; + let containerRootDefinitions; + let mcwsPersistenceProvider; + let mcwsUserContainerProvider; + + beforeEach(() => { + namespaceMIOs = {}; + + spyOn(mcws, 'namespace'); + + mcws.namespace.and.callFake((namespaceUrl) => { + let namespace = namespaceMIOs[namespaceUrl]; + + if (!namespace) { + namespace = namespaceMIOs[namespaceUrl] = + jasmine.createSpyObj( + `namespace:${namespaceUrl}`, + ['read', 'create'] + ); - namespaceMIOs = {}; - - spyOn(mcws, 'namespace'); - - mcws.namespace.and.callFake(function (namespaceUrl) { - var namespace = namespaceMIOs[namespaceUrl]; - - if (!namespace) { - namespace = namespaceMIOs[namespaceUrl] = - jasmine.createSpyObj( - 'namespace:' + namespaceUrl, - ['read', 'create'] - ); - - namespace.created = false; - - namespace.create.and.callFake(function () { - return Promise.resolve() - .then(function () { - namespace.created = true; - return []; - }); - }); - - namespace.read.and.callFake(function () { - if (namespaceUrl.indexOf('missing') !== -1 && - !namespace.created) { - return Promise.reject({ - status: 404 - }); - } - if (namespaceUrl.indexOf('inaccessible') !== -1) { - return Promise.reject({}); - } - return Promise.resolve([]); - }); - } + namespace.created = false; - return namespace; - }); + namespace.create.and.callFake(() => { + return Promise.resolve() + .then(() => { + namespace.created = true; + return []; + }); + }); - mockUserAPI = jasmine.createSpyObj('userAPI', [ - 'getCurrentUser' - ]); + namespace.read.and.callFake(() => { + if (namespaceUrl.includes('missing') && !namespace.created) { + return Promise.reject({ status: 404 }); + } + if (namespaceUrl.includes('inaccessible')) { + return Promise.reject({}); + } + return Promise.resolve([]); + }); + } - mockUserAPI.getCurrentUser - .and.returnValue( - Promise.resolve({ - id: 'someUser', - name: 'someUser' - }) - ); + return namespace; + }); - mockOpenmct = { - user: mockUserAPI - }; + mockUserAPI = jasmine.createSpyObj('userAPI', [ + 'getCurrentUser' + ]); + + mockUserAPI.getCurrentUser + .and.returnValue( + Promise.resolve({ + id: 'someUser', + name: 'someUser' + }) + ); + + mockOpenmct = { + user: mockUserAPI + }; + + sharedRootDefinition = { + id: 'shared:root', + key: 'shared', + name: 'Shared', + url: '/some/shared/namespace' + }; + inaccessibleSharedRootDefinition = { + id: 'inaccessible-shared:root', + key: 'inaccessible-shared', + name: 'Inaccessible Shared', + url: '/inaccessible/shared/namespace' + }; + missingSharedRootDefinition = { + id: 'missing-shared:root', + key: 'missing-shared', + name: 'Missing Shared', + url: '/missing/shared/namespace' + }; + + sharedRootDefinitions = [ + sharedRootDefinition, + inaccessibleSharedRootDefinition, + missingSharedRootDefinition + ]; + + containerRootDefinition = { + id: 'personal', + key: 'personal', + name: 'personal', + url: '/some/personal/namespace', + containsNamespaces: true, + childTemplate: { + id: 'personal-${USER}:root', + key: 'personal-${USER}', + name: '${USER}', + url: '/some/personal/namespace/${USER}' + } + }; + inaccessibleContainerRootDefinition = { + id: 'inaccessible-personal', + key: 'inaccessible-personal', + name: 'inaccessible-personal', + url: '/inaccessible/personal/namespace', + containsNamespaces: true, + childTemplate: { + id: 'inaccessible-personal-${USER}:root', + key: 'inaccessible-personal-${USER}', + name: '${USER}', + url: '/inaccessible/personal/namespace/${USER}' + } + }; + missingContainerRootDefinition = { + id: 'missing-personal:container', + key: 'missing-personal', + name: 'missing-personal', + url: '/missing/personal/namespace', + containsNamespaces: true, + childTemplate: { + id: 'missing-personal-${USER}:root', + key: 'missing-personal-${USER}', + name: '${USER}', + url: '/missing/personal/namespace/${USER}' + } + }; - sharedRootDefinition = { - id: 'shared:root', - key: 'shared', - name: 'Shared', - url: '/some/shared/namespace' - }; - inaccessibleSharedRootDefinition = { - id: 'inaccessible-shared:root', - key: 'inaccessible-shared', - name: 'Inaccessible Shared', - url: '/inaccessible/shared/namespace' - }; - missingSharedRootDefinition = { - id: 'missing-shared:root', - key: 'missing-shared', - name: 'Missing Shared', - url: '/missing/shared/namespace' - }; + containerRootDefinitions = [ + containerRootDefinition, + missingContainerRootDefinition, + inaccessibleContainerRootDefinition + ]; - sharedRootDefinitions = [ - sharedRootDefinition, - inaccessibleSharedRootDefinition, - missingSharedRootDefinition - ]; + mcwsPersistenceProvider = new MCWSPersistenceProvider( + mockOpenmct, + sharedRootDefinitions.concat(containerRootDefinitions) + ); - containerRootDefinition = { - id: 'personal', - key: 'personal', - name: 'personal', - url: '/some/personal/namespace', - containsNamespaces: true, - childTemplate: { - id: 'personal-${USER}:root', - key: 'personal-${USER}', - name: '${USER}', - url: '/some/personal/namespace/${USER}' - } - }; - inaccessibleContainerRootDefinition = { - id: 'inaccessible-personal', - key: 'inaccessible-personal', - name: 'inaccessible-personal', - url: '/inaccessible/personal/namespace', - containsNamespaces: true, - childTemplate: { - id: 'inaccessible-personal-${USER}:root', - key: 'inaccessible-personal-${USER}', - name: '${USER}', - url: '/inaccessible/personal/namespace/${USER}' - } - }; - missingContainerRootDefinition = { - id: 'missing-personal', - key: 'missing-personal', - name: 'missing-personal', - url: '/missing/personal/namespace', - containsNamespaces: true, - childTemplate: { - id: 'missing-personal-${USER}:root', - key: 'missing-personal-${USER}', - name: '${USER}', - url: '/missing/personal/namespace/${USER}' - } - }; + mcwsUserContainerProvider = new MCWSUserContainerProvider( + mockOpenmct, + containerRootDefinitions + ); + }); - containerRootDefinitions = [ - containerRootDefinition, - missingContainerRootDefinition, - inaccessibleContainerRootDefinition - ]; + describe('getRootNamespaces in MCWSUserContainerProvider', () => { + let result; - namespaceService = new MCWSNamespaceService( - mockOpenmct, - $window, - sharedRootDefinitions.concat(containerRootDefinitions) - ); + beforeEach(async () => { + result = await mcwsUserContainerProvider.getRootNamespaces(); }); - describe('getRootNamespaces', function () { - var result; + it('returns container namespaces', () => { + expect(result).toContain(containerRootDefinition); + }); - beforeEach(function (done) { - namespaceService - .getRootNamespaces() - .then(function (namespaceDefinitions) { - result = namespaceDefinitions; - }) - .then(done); - }); + it('creates missing container namespaces ', () => { + expect(result).toContain(missingContainerRootDefinition); + }); - it('returns container namespaces', function () { - expect(result).toContain(containerRootDefinition); - }); + it('removes inaccessible container namespaces', () => { + expect(result) + .not + .toContain(inaccessibleContainerRootDefinition); + }); + }); - it('creates missing container namespaces ', function () { - expect(result).toContain(missingContainerRootDefinition); - }); + describe('getRootNamespaces in MCWSPersistenceProvider', () => { + let result; - it('removes inaccessible container namespaces', function () { - expect(result) - .not - .toContain(inaccessibleContainerRootDefinition); - }); + beforeEach(async () => { + result = await mcwsPersistenceProvider.getRootNamespaces(); + }); - it('returns normal namespaces', function () { - expect(result).toContain(sharedRootDefinition); - }); + it('returns normal namespaces', () => { + expect(result).toContain(sharedRootDefinition); + }); - it('creates missing normal namespaces ', function () { - expect(result).toContain(missingSharedRootDefinition); - }); + it('creates missing normal namespaces ', () => { + expect(result).toContain(missingSharedRootDefinition); + }); - it('removes inaccessible normal namespaces', function () { - expect(result).not.toContain(inaccessibleSharedRootDefinition); - }); + it('removes inaccessible normal namespaces', () => { + expect(result).not.toContain(inaccessibleSharedRootDefinition); }); + }); - describe('getContainedNamespaces', function () { - var userRootNamespaceMIO, - userNamespaceMIO, - otherUserNamespaceMIO; + describe('getContainedNamespaces', () => { + let userRootNamespaceMIO, + userNamespaceMIO, + otherUserNamespaceMIO; + + beforeEach(() => { + const namespaceMIOs = {}; + mcws.namespace.and.callFake((namespaceUrl) => namespaceMIOs[namespaceUrl]); + + userRootNamespaceMIO = + namespaceMIOs['/some/personal/namespace'] = + jasmine.createSpyObj( + 'userNamespace', + ['read', 'create'] + ); + userRootNamespaceMIO.read.and.returnValue(Promise.resolve([])); - beforeEach(function () { - var namespaceMIOs = {}; - mcws.namespace.and.callFake(function (namespaceUrl) { - return namespaceMIOs[namespaceUrl]; - }); - userRootNamespaceMIO = - namespaceMIOs['/some/personal/namespace'] = - jasmine.createSpyObj( - 'userNamespace', - ['read', 'create'] - ); - userRootNamespaceMIO.read.and.returnValue(Promise.resolve([])); + userNamespaceMIO = + namespaceMIOs['/some/personal/namespace/someUser'] = + jasmine.createSpyObj( + 'userNamespaceMIO', + ['read', 'create'] + ); + userNamespaceMIO.read.and.returnValue(Promise.resolve([])); - userNamespaceMIO = - namespaceMIOs['/some/personal/namespace/someUser'] = - jasmine.createSpyObj( - 'userNamespaceMIO', - ['read', 'create'] - ); - userNamespaceMIO.read.and.returnValue(Promise.resolve([])); + otherUserNamespaceMIO = + namespaceMIOs['/some/personal/namespace/otherUser'] = + jasmine.createSpyObj( + 'otherUserNamespace', + ['read', 'create'] + ); - otherUserNamespaceMIO = - namespaceMIOs['/some/personal/namespace/otherUser'] = - jasmine.createSpyObj( - 'otherUserNamespace', - ['read', 'create'] - ); + otherUserNamespaceMIO.read.and.returnValue(Promise.resolve([])); + }); - otherUserNamespaceMIO.read.and.returnValue(Promise.resolve([])); - }); + function expectCurrentUserDefinition(definition) { + expect(definition.id).toBe('personal-someUser:root'); + expect(definition.key).toBe('personal-someUser'); + expect(definition.name).toBe('someUser'); + expect(definition.url).toBe('/some/personal/namespace/someUser'); + expect(definition.location).toBe('personal'); + }; + + function expectOtherUserDefinition(definition) { + expect(definition.id).toBe('personal-otherUser:root'); + expect(definition.key).toBe('personal-otherUser'); + expect(definition.name).toBe('otherUser'); + expect(definition.url).toBe('/some/personal/namespace/otherUser'); + expect(definition.location).toBe('personal'); + }; + + it('returns contents with current user first', async () => { + userRootNamespaceMIO.read.and.returnValue(Promise.resolve([ + { + object: 'namespace', + subject: '/some/personal/namespace/otherUser' + }, + { + object: 'namespace', + subject: '/some/personal/namespace/someUser' + } + ])); - function expectCurrentUserDefinition(definition) { - expect(definition.id).toBe('personal-someUser:root'); - expect(definition.key).toBe('personal-someUser'); - expect(definition.name).toBe('someUser'); - expect(definition.url) - .toBe('/some/personal/namespace/someUser'); - expect(definition.location).toBe('personal'); - } + const contents = await mcwsUserContainerProvider.getContainedNamespaces(containerRootDefinition); + expect(userNamespaceMIO.create).not.toHaveBeenCalled(); + expect(otherUserNamespaceMIO.create).not.toHaveBeenCalled(); - function expectOtherUserDefinition(definition) { - expect(definition.id).toBe('personal-otherUser:root'); - expect(definition.key).toBe('personal-otherUser'); - expect(definition.name).toBe('otherUser'); - expect(definition.url) - .toBe('/some/personal/namespace/otherUser'); - expect(definition.location).toBe('personal'); - } + expect(contents.length).toBe(2); + expectCurrentUserDefinition(contents[0]); + expectOtherUserDefinition(contents[1]); + }); - it('returns contents with current user first', function (done) { - userRootNamespaceMIO.read.and.returnValue(Promise.resolve([ - { - object: 'namespace', - subject: '/some/personal/namespace/otherUser' - }, - { - object: 'namespace', - subject: '/some/personal/namespace/someUser' - } - ])); - - namespaceService - .getContainedNamespaces(containerRootDefinition) - .then(function (contents) { - expect(userNamespaceMIO.create) - .not - .toHaveBeenCalled(); - expect(otherUserNamespaceMIO.create) - .not - .toHaveBeenCalled(); - - expect(contents.length).toBe(2); - expectCurrentUserDefinition(contents[0]); - expectOtherUserDefinition(contents[1]); - }) - .then(done); - }); + it('creates user if missing', async () => { + userRootNamespaceMIO.read.and.returnValue(Promise.resolve([])); + userNamespaceMIO.read.and.returnValue(Promise.reject({status: 404})); + userNamespaceMIO.create.and.returnValue(Promise.resolve([])); - it('creates user if missing', function (done) { - userRootNamespaceMIO.read.and.returnValue(Promise.resolve([])); - userNamespaceMIO.read.and.returnValue(Promise.reject({status: 404})); - userNamespaceMIO.create.and.returnValue(Promise.resolve([])); - - namespaceService - .getContainedNamespaces(containerRootDefinition) - .then(function (contents) { - expect(contents.length).toBe(1); - expect(userNamespaceMIO.create) - .toHaveBeenCalled(); - expectCurrentUserDefinition(contents[0]); - }) - .then(done); - }); + const contents = await mcwsUserContainerProvider.getContainedNamespaces(containerRootDefinition); + expect(contents.length).toBe(1); + expect(userNamespaceMIO.create).toHaveBeenCalled(); + expectCurrentUserDefinition(contents[0]); }); + }); - describe('namespace filtering', function () { - var smapDefinition, - mslDefinition, - ammosDefinition, - filterDefinitions; - - beforeEach(function () { - smapDefinition = { - key: 'smap-thing', - url: '/path/to/smap-namespace' - }; - mslDefinition = { - key: 'msl-thing', - url: '/path/to/msl-namespace' - }; - ammosDefinition = { - key: 'ammos-thing', - url: '/path/to/ammos-namespace' - }; - filterDefinitions = [ - smapDefinition, - mslDefinition, - ammosDefinition - ]; - - namespaceService = new MCWSNamespaceService( - mockOpenmct, - $window, - filterDefinitions - ); + // need to fix this (issue is mocking window.location.pathname) + xdescribe('namespace filtering', () => { + let smapDefinition; + let mslDefinition; + let ammosDefinition; + let filterDefinitions; + let filterPath = '/mcws/clients/vista/'; + let filterTerm = 'vista'; + + beforeEach(() => { + spyOn(String.prototype, 'startsWith').and.callFake(function(searchString) { + if (searchString.includes('/') && searchString === filterPath) { + console.log(`path: ${searchString} === ${filterPath}`); + return true; + } else if (searchString.includes(filterTerm)) { + console.log(`term: ${searchString} === ${filterTerm}`); + return true; + } + return false; }); - it('does not filter with default path', function (done) { - namespaceService - .getRootNamespaces() - .then(function (namespaces) { - expect(namespaces).toEqual(filterDefinitions); - }) - .then(done); - }); + smapDefinition = { + key: 'smap-thing', + url: '/path/to/smap-namespace' + }; + mslDefinition = { + key: 'msl-thing', + url: '/path/to/msl-namespace' + }; + ammosDefinition = { + key: 'ammos-thing', + url: '/path/to/ammos-namespace' + }; + filterDefinitions = [ + smapDefinition, + mslDefinition, + ammosDefinition + ]; - it('only includes smap with smap-path', function (done) { - $window.location.pathname = '/mcws/clients/vista-smap/'; - namespaceService - .getRootNamespaces() - .then(function (namespaces) { - expect(namespaces).toEqual([smapDefinition]); - }) - .then(done); - }); + mcwsPersistenceProvider = new MCWSPersistenceProvider( + mockOpenmct, + filterDefinitions + ); + }); - it('only includes msl with msl-path', function (done) { - $window.location.pathname = '/mcws/clients/vista-msl/'; - namespaceService - .getRootNamespaces() - .then(function (namespaces) { - expect(namespaces).toEqual([mslDefinition]); - }) - .then(done); - }); + afterEach(() => { + String.prototype.startsWith.and.callThrough(); + }); - it('only includes ammos with ammos-path', function (done) { - $window.location.pathname = '/mcws/clients/vista-ammos/'; - namespaceService - .getRootNamespaces() - .then(function (namespaces) { - expect(namespaces).toEqual([ammosDefinition]); - }) - .then(done); - }); + it('does not filter with default path', async () => { + const namespaces = await mcwsPersistenceProvider.getRootNamespaces(); + expect(namespaces).toEqual(filterDefinitions); + }); + + it('only includes smap with smap-path', async () => { + filterPath = '/mcws/clients/vista-smap'; + filterTerm = 'smap'; + const namespaces = await mcwsPersistenceProvider.getRootNamespaces(); + expect(namespaces).toEqual([smapDefinition]); + }); + + it('only includes msl with msl-path', async () => { + filterPath = '/mcws/clients/vista-msl'; + filterTerm = 'msl'; + const namespaces = await mcwsPersistenceProvider.getRootNamespaces(); + expect(namespaces).toEqual([mslDefinition]); + }); + + it('only includes ammos with ammos-path', async () => { + filterPath = '/mcws/clients/vista-ammos'; + filterTerm = 'ammos'; + const namespaces = await mcwsPersistenceProvider.getRootNamespaces(); + expect(namespaces).toEqual([ammosDefinition]); }); }); });