reactant-module
Version:
A module model for Reactant
120 lines (110 loc) • 2.62 kB
text/typescript
import { injectable, createContainer, createStore, action, state } from '../..';
describe('@state', () => {
test('base', () => {
()
class Counter {
count = 0;
increase() {
this.count += 1;
}
}
const ServiceIdentifiers = new Map();
const modules = [Counter];
const container = createContainer({
ServiceIdentifiers,
modules,
options: {
defaultScope: 'Singleton',
},
});
const counter = container.get(Counter);
const store = createStore({
modules,
container,
ServiceIdentifiers,
loadedModules: new Set(),
load: (...args: any[]) => {
//
},
dynamicModules: new Map(),
pluginHooks: {
middleware: [],
beforeCombineRootReducers: [],
afterCombineRootReducers: [],
enhancer: [],
preloadedStateHandler: [],
afterCreateStore: [],
provider: [],
},
});
expect(counter.count).toBe(0);
expect(Object.values(store.getState())).toEqual([{ count: 0 }]);
counter.increase();
expect(counter.count).toBe(1);
});
test('inheritance', () => {
()
class BaseCounter {
count = 0;
increase() {
this.count += 1;
}
}
()
class Counter0 extends BaseCounter {
counter0 = 0;
count = 0;
increase() {
this.count += 1;
}
}
()
class Counter extends BaseCounter {
counter = 0;
count = 10;
}
const ServiceIdentifiers = new Map();
const modules = [Counter, Counter0];
const container = createContainer({
ServiceIdentifiers,
modules,
options: {
defaultScope: 'Singleton',
},
});
const counter = container.get(Counter);
const store = createStore({
modules,
container,
ServiceIdentifiers,
loadedModules: new Set(),
load: (...args: any[]) => {},
dynamicModules: new Map(),
pluginHooks: {
middleware: [],
beforeCombineRootReducers: [],
afterCombineRootReducers: [],
enhancer: [],
preloadedStateHandler: [],
afterCreateStore: [],
provider: [],
},
});
expect(counter.count).toBe(10);
counter.increase();
expect(counter.count).toBe(11);
expect(Object.values(store.getState())).toEqual([
{ count: 11, counter: 0 },
{ count: 0, counter0: 0 },
]);
});
});