reactant-module
Version:
A module model for Reactant
90 lines (82 loc) • 2.02 kB
text/typescript
import {
injectable,
createContainer,
state,
createStore,
action,
computed,
} from '../..';
describe('@computed', () => {
test('inheritance', () => {
const computedFn = jest.fn();
const computedFn1 = jest.fn();
()
class BaseCounter {
count = 0;
increase() {
this.count += 1;
}
(({ count }: BaseCounter) => [count])
get num() {
computedFn();
return this.count + 1;
}
}
()
class Counter extends BaseCounter {
count1 = 0;
increase1() {
this.count1 += 1;
}
(({ count, count1 }: Counter) => [count, count1])
get num1() {
computedFn1();
return this.count1 + this.num;
}
}
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(computedFn.mock.calls.length).toBe(0);
expect(computedFn1.mock.calls.length).toBe(0);
counter.increase();
expect(counter.num).toBe(2);
expect(counter.num1).toBe(2);
expect(computedFn.mock.calls.length).toBe(1);
expect(computedFn1.mock.calls.length).toBe(1);
counter.increase1();
expect(counter.num1).toBe(3);
expect(computedFn.mock.calls.length).toBe(1);
expect(computedFn1.mock.calls.length).toBe(2);
});
});