* refactor: 💡 postmessage pattern -> contextbridge boundary api * refactor: 💡 use sinon stub for boundary api mock test helper * refactor: 💡 updated tests * refactor: 💡 renamed boundary mock api * chore: 🤖 addressed feedback * refactor: 💡 updated context bridge name, added nested objects
108 lines
3.7 KiB
JavaScript
108 lines
3.7 KiB
JavaScript
/**
|
|
* Copyright IBM Corp. 2021, 2026
|
|
* SPDX-License-Identifier: BUSL-1.1
|
|
*/
|
|
|
|
import sinon from 'sinon';
|
|
import { typeOf } from '@ember/utils';
|
|
import { resourceNames } from 'api/handlers/cache-daemon-handler';
|
|
import { singularize } from 'ember-inflector';
|
|
import { underscore } from '@ember/string';
|
|
|
|
/**
|
|
* This test helper can be used to help setup your sinon stubs in your tests.
|
|
* Must be called after setupMirage and setupDesktopContextBridgeApiMock in tests.
|
|
*
|
|
* ```js
|
|
* import setupStubs from 'api/test-support/handlers/cache-daemon-search';
|
|
*
|
|
* module('Acceptance | my test', function(hooks) {
|
|
* setupApplicationTest(hooks);
|
|
* setupMirage(hooks);
|
|
* setupDesktopContextBridgeApiMock(hooks);
|
|
* setupStubs(hooks);
|
|
*
|
|
* // add your actual tests here
|
|
* });
|
|
* ```
|
|
*
|
|
* This will setup a stub for window.desktop.daemon.searchCacheDaemon and include
|
|
* helper methods to help setup what the cache daemon search command returns.
|
|
*
|
|
* @module Test Helpers
|
|
* @public
|
|
*/
|
|
export default function setupStubs(hooks) {
|
|
let stubTypes = [];
|
|
|
|
hooks.beforeEach(function () {
|
|
/**
|
|
* We aim to still use mirage data when stubbing out the cache daemon
|
|
* search. However, the handler expects the search data to be raw JSON from
|
|
* the API which will get serialized. We add back the scope object for it to
|
|
* get correctly normalized and other fields such as "attributes" from the
|
|
* API will already be hoisted and will not be re-normalized from mirage data.
|
|
*
|
|
* This convenience method can take in multiple types,
|
|
* e.g. this.stubCacheDaemonSearch('targets', 'sessions').
|
|
* This will have sinon stub out each call so the first call to search will
|
|
* provide all the models from the first parameter you specify,
|
|
* the second call will provide all the models from the second argument, etc.
|
|
* @param types
|
|
*/
|
|
this.stubCacheDaemonSearch = (...types) => {
|
|
stubTypes = types;
|
|
|
|
types.forEach((type, i) => {
|
|
this.searchCacheDaemonStub.onCall(i).callsFake(() => {
|
|
let models;
|
|
let resourceName;
|
|
if (typeOf(type) === 'object') {
|
|
models = type.func();
|
|
resourceName = type.resource;
|
|
} else {
|
|
models = this.server.schema[type].all().models;
|
|
resourceName = type;
|
|
}
|
|
|
|
return {
|
|
[underscore(resourceNames[singularize(resourceName)])]: models.map(
|
|
(model) => {
|
|
// Use internal serializer to serialize the model correctly
|
|
// according to our mirage serializers
|
|
const modelData =
|
|
this.server.serializerOrRegistry.serialize(model);
|
|
|
|
// Serialize the data properly to standard JSON as that is what
|
|
// we're expecting from the cache daemon response
|
|
return JSON.parse(JSON.stringify(modelData));
|
|
},
|
|
),
|
|
};
|
|
});
|
|
});
|
|
};
|
|
});
|
|
|
|
hooks.afterEach(function () {
|
|
// Verify the number of mocks we set up match with how
|
|
// many times the cache daemon was called with
|
|
sinon.assert.callCount(this.searchCacheDaemonStub, stubTypes.length);
|
|
|
|
stubTypes.forEach((type, i) => {
|
|
// Assert that the stub was called with the resource we're trying to mock
|
|
// so we don't mistakenly mock the wrong resource
|
|
const call = this.searchCacheDaemonStub.getCall(i);
|
|
let resource = type;
|
|
if (typeOf(type) === 'object') {
|
|
resource = type.resource;
|
|
}
|
|
|
|
const mappedResourceName = resourceNames[singularize(resource)];
|
|
sinon.assert.match(call.args[0], { resource: mappedResourceName });
|
|
});
|
|
|
|
sinon.restore();
|
|
});
|
|
}
|