Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit c4a9973

Browse files
committed
added key to agile instance
1 parent 4ca6166 commit c4a9973

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

packages/core/src/agile.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ import {
3030
export class Agile {
3131
public config: AgileConfigInterface;
3232

33+
// Key/Name identifier of Agile Instance
34+
public key?: AgileKey;
35+
3336
// Queues and executes incoming Observer-based Jobs
3437
public runtime: Runtime;
3538
// Manages and simplifies the subscription to UI-Components
@@ -87,6 +90,7 @@ export class Agile {
8790
this.config = {
8891
waitForMount: config.waitForMount as any,
8992
};
93+
this.key = config.key;
9094
this.integrations = new Integrations(this);
9195
this.runtime = new Runtime(this);
9296
this.subController = new SubController(this);
@@ -151,7 +155,7 @@ export class Agile {
151155
* @param config - Configuration object
152156
*/
153157
public createStorage(config: CreateStorageConfigInterface): Storage {
154-
return createStorage({ ...config, ...{ agileInstance: this } });
158+
return createStorage(config);
155159
}
156160

157161
/**
@@ -476,6 +480,8 @@ export function createComputed<ComputedValueType = any>(
476480
);
477481
}
478482

483+
export type AgileKey = string | number;
484+
479485
export interface CreateAgileSubInstanceInterface {
480486
/**
481487
* Instance of Agile the Instance belongs to.
@@ -521,6 +527,11 @@ export interface CreateAgileConfigInterface {
521527
* @default false
522528
*/
523529
bindGlobal?: boolean;
530+
/**
531+
* Key/Name identifier of Agile Instance.
532+
* @default undefined
533+
*/
534+
key?: AgileKey;
524535
}
525536

526537
export interface AgileConfigInterface {

packages/core/src/shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import { Agile } from './agile';
22

33
// Shared Agile Instance that is used when no Agile Instance was specified
44
// eslint-disable-next-line prefer-const
5-
export let shared = new Agile();
5+
export let shared = new Agile({ key: 'shared' });

packages/core/tests/unit/agile.test.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,6 @@ import { LogMock } from '../helper/logMock';
1515
import * as Utils from '../../src/utils';
1616

1717
// https://github.com/facebook/jest/issues/5023
18-
// https://medium.com/@masonlgoetz/mock-static-class-methods-in-jest-1ceda967b47f
19-
// https://gist.github.com/virgs/d9c50e878fc69832c01f8085f2953f12
20-
jest.mock('../../src/integrations', () => {
21-
const mockedInstances = {
22-
Integrations: jest.fn().mockImplementation(() => {
23-
return {
24-
integrate: jest.fn(),
25-
};
26-
}),
27-
};
28-
// @ts-ignore
29-
mockedInstances.Integrations.onRegisteredExternalIntegration = jest.fn();
30-
return mockedInstances;
31-
});
3218
jest.mock('../../src/runtime', () => {
3319
return {
3420
Runtime: jest.fn(),
@@ -44,12 +30,34 @@ jest.mock('../../src/storages', () => {
4430
Storages: jest.fn(),
4531
};
4632
});
33+
34+
// https://gist.github.com/virgs/d9c50e878fc69832c01f8085f2953f12
35+
// https://medium.com/@masonlgoetz/mock-static-class-methods-in-jest-1ceda967b47f
36+
jest.mock('../../src/integrations', () => {
37+
const mockedInstances = {
38+
// https://jestjs.io/docs/mock-function-api#mockfnmockimplementationfn
39+
Integrations: jest.fn().mockImplementation(() => {
40+
return {
41+
integrate: jest.fn(),
42+
hasIntegration: jest.fn(),
43+
};
44+
}),
45+
};
46+
// @ts-ignore
47+
mockedInstances.Integrations.onRegisteredExternalIntegration = jest.fn();
48+
return mockedInstances;
49+
});
50+
4751
jest.mock('../../src/storages/storage');
4852
jest.mock('../../src/collection');
4953
jest.mock('../../src/computed');
50-
// Can't mock State because mocks get instantiated before everything else
51-
// -> I got the good old not loaded Object error https://github.com/kentcdodds/how-jest-mocking-works
52-
// jest.mock("../../src/state/index");
54+
55+
// https://github.com/facebook/jest/issues/5023
56+
jest.mock('../../src/state', () => {
57+
return {
58+
State: jest.fn(),
59+
};
60+
});
5361

5462
describe('Agile Tests', () => {
5563
const RuntimeMock = Runtime as jest.MockedClass<typeof Runtime>;
@@ -84,6 +92,7 @@ describe('Agile Tests', () => {
8492
expect(agile.config).toStrictEqual({
8593
waitForMount: true,
8694
});
95+
expect(agile.key).toBeUndefined();
8796
expect(IntegrationsMock).toHaveBeenCalledWith(agile);
8897
// expect(agile.integrations).toBeInstanceOf(Integrations); // Because 'Integrations' is completely overwritten with a mock
8998
expect(RuntimeMock).toHaveBeenCalledWith(agile);
@@ -113,12 +122,14 @@ describe('Agile Tests', () => {
113122
timestamp: true,
114123
},
115124
bindGlobal: true,
125+
key: 'jeff',
116126
});
117127

118128
// Check if Agile properties got instantiated properly
119129
expect(agile.config).toStrictEqual({
120130
waitForMount: false,
121131
});
132+
expect(agile.key).toBe('jeff');
122133
expect(IntegrationsMock).toHaveBeenCalledWith(agile);
123134
// expect(agile.integrations).toBeInstanceOf(Integrations); // Because 'Integrations' is completely overwritten with a mock
124135
expect(RuntimeMock).toHaveBeenCalledWith(agile);
@@ -358,6 +369,10 @@ describe('Agile Tests', () => {
358369
});
359370

360371
describe('hasStorage function tests', () => {
372+
beforeEach(() => {
373+
agile.storages.hasStorage = jest.fn();
374+
});
375+
361376
it('should check if Agile has any registered Storage', () => {
362377
agile.hasStorage();
363378

0 commit comments

Comments
 (0)