Skip to content

Commit

Permalink
Fix component mocking not re-creating instances
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe authored and andyholmes committed Apr 9, 2024
1 parent 719e727 commit 9cc0a53
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 8 additions & 3 deletions installed-tests/fixtures/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,16 @@ function isolateDirectories() {
* Patch in the mock components for plugin tests.
*/
export async function mockComponents() {
const {components} = await import(`file://${Config.PACKAGE_DATADIR}/service/components/index.js`);
const {functionOverrides} = await import(`file://${Config.PACKAGE_DATADIR}/service/components/index.js`);
const MockComponents = await import('./components/index.js');

for (const [name, module] of Object.entries(MockComponents))
components[name] = module;
functionOverrides.acquire = function (name) {
return new MockComponents[name].default();
};

functionOverrides.release = function (name) {
return null;
};
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/service/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import * as sound from './sound.js';
import * as upower from './upower.js';
import * as ydotool from './ydotool.js';

export const components = {
export const functionOverrides = {};

const components = {
atspi,
clipboard,
contacts,
Expand Down Expand Up @@ -42,6 +44,9 @@ const Default = new Map();
* @return {*} The default instance of a component
*/
export function acquire(name) {
if (functionOverrides.acquire)
return functionOverrides.acquire(name);

let component;

try {
Expand Down Expand Up @@ -76,6 +81,9 @@ export function acquire(name) {
* @return {null} A %null value, useful for overriding a traced variable
*/
export function release(name) {
if (functionOverrides.release)
return functionOverrides.release(name);

try {
const info = Default.get(name);

Expand Down

0 comments on commit 9cc0a53

Please sign in to comment.