|
| 1 | +import { expect } from "chai"; |
| 2 | +import { mix } from "mixwith"; |
| 3 | + |
| 4 | +import { |
| 5 | + ApplicationContextMixin, |
| 6 | + ContextProvider, |
| 7 | + createAndPatchRegistry, |
| 8 | + MaterialContextMixin, |
| 9 | +} from "../src/context"; |
| 10 | + |
| 11 | +class MockMaterial { |
| 12 | + static createDefault() { |
| 13 | + return "defaultMockMaterial"; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +class SpecificMockMaterial { |
| 18 | + static createDefault() { |
| 19 | + return "defaultSpecificMockMaterial"; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +class MockApplication { |
| 24 | + static createDefault() { |
| 25 | + return "defaultMockApplication"; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class SpecificMockApplication { |
| 30 | + static createDefault() { |
| 31 | + return "defaultSpecificMockApplication"; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +class ProviderEntity extends mix(ContextProvider).with( |
| 36 | + MaterialContextMixin, |
| 37 | + ApplicationContextMixin, |
| 38 | +) { |
| 39 | + static Material = MockMaterial; |
| 40 | + |
| 41 | + static Application = MockApplication; |
| 42 | +} |
| 43 | + |
| 44 | +class DerivedProviderEntity extends ProviderEntity { |
| 45 | + static Material = SpecificMockMaterial; |
| 46 | + |
| 47 | + static Application = SpecificMockApplication; |
| 48 | +} |
| 49 | + |
| 50 | +class ApplicationContextProvider extends mix(ContextProvider).with(ApplicationContextMixin) { |
| 51 | + static Application = SpecificMockApplication; |
| 52 | +} |
| 53 | + |
| 54 | +describe("Material & Application ContextMixin", () => { |
| 55 | + const config = {}; |
| 56 | + |
| 57 | + it("uses static entity class", () => { |
| 58 | + const provider = new ProviderEntity(config); |
| 59 | + expect(provider.material).to.be.equal("defaultMockMaterial"); |
| 60 | + expect(provider.application).to.be.equal("defaultMockApplication"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("uses static entity class from derived class", () => { |
| 64 | + const provider = new DerivedProviderEntity(config); |
| 65 | + expect(provider.material).to.be.equal("defaultSpecificMockMaterial"); |
| 66 | + expect(provider.application).to.be.equal("defaultSpecificMockApplication"); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("ContextProviderRegistryContainer", () => { |
| 71 | + const classConfigObj = { |
| 72 | + DataManager: { |
| 73 | + providerCls: ProviderEntity, |
| 74 | + config: { name: "example1", domain: "important" }, |
| 75 | + }, |
| 76 | + ApplicationDataManager: { |
| 77 | + providerCls: ApplicationContextProvider, |
| 78 | + config: { name: "example2", domain: "important" }, |
| 79 | + }, |
| 80 | + }; |
| 81 | + |
| 82 | + it("can be created and patched", () => { |
| 83 | + const registry = createAndPatchRegistry(classConfigObj, { Material: SpecificMockMaterial }); |
| 84 | + const _dataProvider = registry.findProviderInstanceByName("DataManager"); |
| 85 | + const dataProvider = new _dataProvider.constructor(_dataProvider.config); |
| 86 | + const _appProvider = registry.findProviderInstanceByName("ApplicationDataManager"); |
| 87 | + const appProvider = new _appProvider.constructor(_appProvider.config); |
| 88 | + expect(dataProvider.material).to.be.equal("defaultSpecificMockMaterial"); |
| 89 | + expect(appProvider.application).to.be.equal("defaultSpecificMockApplication"); |
| 90 | + }); |
| 91 | +}); |
0 commit comments