|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { META_REDUCERS, MetaReducer, Store, StoreModule } from '@ngrx/store'; |
| 3 | +import { EffectsModule, EffectSources } from '@ngrx/effects'; |
| 4 | + |
| 5 | +import { EntityEffects } from './effects/effects-all'; |
| 6 | +import { ExtraEffects } from './effects/effects-extra'; |
| 7 | +import { EntityIfNecessaryOperators } from './effects/if-necessary-operators'; |
| 8 | +import { EntityOperators } from './effects/operators'; |
| 9 | +import { autoEntityMetaReducer } from './reducer/meta-reducer'; |
| 10 | +import { |
| 11 | + NgRxAutoEntityFeatureModule, |
| 12 | + NgrxAutoEntityModule, |
| 13 | + NgRxAutoEntityRootModuleNoEffects, |
| 14 | + NgRxAutoEntityRootModuleNoEntityEffects, |
| 15 | + NgRxAutoEntityRootModuleWithEffects |
| 16 | +} from './module'; |
| 17 | +import { NEVER } from 'rxjs'; |
| 18 | +import { NgrxAutoEntityService } from './service/service'; |
| 19 | +import { NGRX_AUTO_ENTITY_APP_STORE } from './effects/if-necessary-operator-utils'; |
| 20 | + |
| 21 | +let effectSources: EffectSources; |
| 22 | +let entityService: NgrxAutoEntityService | null; |
| 23 | +let entityOps: EntityOperators | null; |
| 24 | +let entityIfNecessaryOps: EntityIfNecessaryOperators | null; |
| 25 | +let entityEffects: EntityEffects | null; |
| 26 | +let extraEffects: ExtraEffects | null; |
| 27 | +let metaReducers: MetaReducer[] | null; |
| 28 | +let appStore: Store | null; |
| 29 | + |
| 30 | +beforeEach(() => { |
| 31 | + effectSources = null; |
| 32 | + entityService = null; |
| 33 | + entityOps = null; |
| 34 | + entityIfNecessaryOps = null; |
| 35 | + entityEffects = null; |
| 36 | + extraEffects = null; |
| 37 | + metaReducers = null; |
| 38 | + appStore = null; |
| 39 | +}); |
| 40 | + |
| 41 | +function resolveDependencies() { |
| 42 | + effectSources = TestBed.inject(EffectSources); |
| 43 | + entityService = TestBed.inject(NgrxAutoEntityService, null, { optional: true }); |
| 44 | + entityOps = TestBed.inject(EntityOperators, null, { optional: true }); |
| 45 | + entityIfNecessaryOps = TestBed.inject(EntityIfNecessaryOperators, null, { optional: true }); |
| 46 | + entityEffects = TestBed.inject(EntityEffects, null, { optional: true }); |
| 47 | + extraEffects = TestBed.inject(ExtraEffects, null, { optional: true }); |
| 48 | + metaReducers = TestBed.inject(META_REDUCERS, null, { optional: true }); |
| 49 | + appStore = TestBed.inject(NGRX_AUTO_ENTITY_APP_STORE, null, { optional: true }) as Store; |
| 50 | +} |
| 51 | + |
| 52 | +const mockEffectSourcesProvider = { |
| 53 | + useValue: { |
| 54 | + addEffects: jest.fn(), |
| 55 | + toActions: jest.fn().mockReturnValue(NEVER) |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +describe('Module: NgRxAutoEntityRootModuleWithEffects', () => { |
| 60 | + it('should provide the correct dependencies', () => { |
| 61 | + TestBed.configureTestingModule({ |
| 62 | + imports: [StoreModule.forRoot({}), EffectsModule.forRoot([]), NgrxAutoEntityModule.forRoot()] |
| 63 | + }).overrideProvider(EffectSources, mockEffectSourcesProvider); |
| 64 | + |
| 65 | + resolveDependencies(); |
| 66 | + |
| 67 | + expect(entityService).toBeDefined(); |
| 68 | + expect(entityOps).toBeDefined(); |
| 69 | + expect(entityIfNecessaryOps).toBeDefined(); |
| 70 | + expect(entityEffects).toBeDefined(); |
| 71 | + expect(extraEffects).toBeDefined(); |
| 72 | + expect(appStore).toBeNull(); |
| 73 | + |
| 74 | + expect(metaReducers).toContain(autoEntityMetaReducer); |
| 75 | + |
| 76 | + expect(effectSources.addEffects).toHaveBeenCalledWith(entityEffects); |
| 77 | + expect(effectSources.addEffects).toHaveBeenCalledWith(extraEffects); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe('Module: NgRxAutoEntityRootModuleNoEntityEffects', () => { |
| 82 | + it('should provide the correct dependencies', () => { |
| 83 | + TestBed.configureTestingModule({ |
| 84 | + imports: [StoreModule.forRoot({}), EffectsModule.forRoot([]), NgrxAutoEntityModule.forRootNoEntityEffects()] |
| 85 | + }).overrideProvider(EffectSources, mockEffectSourcesProvider); |
| 86 | + |
| 87 | + resolveDependencies(); |
| 88 | + |
| 89 | + expect(entityService).toBeDefined(); |
| 90 | + expect(entityOps).toBeDefined(); |
| 91 | + expect(entityIfNecessaryOps).toBeDefined(); |
| 92 | + expect(entityEffects).toBeNull(); |
| 93 | + expect(extraEffects).toBeDefined(); |
| 94 | + expect(appStore).toBeNull(); |
| 95 | + |
| 96 | + expect(metaReducers).toContain(autoEntityMetaReducer); |
| 97 | + |
| 98 | + expect(effectSources.addEffects).not.toHaveBeenCalledWith(entityEffects); |
| 99 | + expect(effectSources.addEffects).toHaveBeenCalledWith(extraEffects); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('Module: NgRxAutoEntityRootModuleNoEffects', () => { |
| 104 | + it('should provide the correct dependencies', () => { |
| 105 | + TestBed.configureTestingModule({ |
| 106 | + imports: [StoreModule.forRoot({}), EffectsModule.forRoot([]), NgrxAutoEntityModule.forRootNoEffects()] |
| 107 | + }).overrideProvider(EffectSources, mockEffectSourcesProvider); |
| 108 | + |
| 109 | + const effectSources = TestBed.inject(EffectSources); |
| 110 | + |
| 111 | + resolveDependencies(); |
| 112 | + |
| 113 | + expect(entityService).toBeDefined(); |
| 114 | + expect(entityOps).toBeDefined(); |
| 115 | + expect(entityIfNecessaryOps).toBeDefined(); |
| 116 | + expect(entityEffects).toBeNull(); |
| 117 | + expect(extraEffects).toBeNull(); |
| 118 | + expect(appStore).toBeNull(); |
| 119 | + |
| 120 | + expect(metaReducers).toContain(autoEntityMetaReducer); |
| 121 | + |
| 122 | + expect(effectSources.addEffects).not.toHaveBeenCalledWith(entityEffects); |
| 123 | + expect(effectSources.addEffects).not.toHaveBeenCalledWith(extraEffects); |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
| 127 | +describe('Module: NgRxAutoEntityFeatureModule', () => { |
| 128 | + it('should provide the correct dependencies', () => { |
| 129 | + TestBed.configureTestingModule({ |
| 130 | + imports: [NgrxAutoEntityModule.forFeature()] |
| 131 | + }); |
| 132 | + }); |
| 133 | +}); |
| 134 | + |
| 135 | +describe('Module: NgrxAutoEntityModule', () => { |
| 136 | + describe('Method: forRoot', () => { |
| 137 | + it('should return the NgRxAutoEntityRootModuleWithEffects module with providers', () => { |
| 138 | + const module = NgrxAutoEntityModule.forRoot(); |
| 139 | + expect(module).toEqual({ |
| 140 | + ngModule: NgRxAutoEntityRootModuleWithEffects, |
| 141 | + providers: expect.any(Array) |
| 142 | + }); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe('Method: forRootNoEntityEffects', () => { |
| 147 | + it('should return the NgRxAutoEntityRootModuleNoEntityEffects module with providers', () => { |
| 148 | + const module = NgrxAutoEntityModule.forRootNoEntityEffects(); |
| 149 | + expect(module).toEqual({ |
| 150 | + ngModule: NgRxAutoEntityRootModuleNoEntityEffects, |
| 151 | + providers: expect.any(Array) |
| 152 | + }); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe('Method: forRootNoEffects', () => { |
| 157 | + it('should return the NgRxAutoEntityRootModuleNoEffects module with providers', () => { |
| 158 | + const module = NgrxAutoEntityModule.forRootNoEffects(); |
| 159 | + expect(module).toEqual({ |
| 160 | + ngModule: NgRxAutoEntityRootModuleNoEffects, |
| 161 | + providers: expect.any(Array) |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + describe('Method: forFeature', () => { |
| 167 | + it('should return the NgRxAutoEntityFeatureModule module with providers', () => { |
| 168 | + const module = NgrxAutoEntityModule.forFeature(); |
| 169 | + expect(module).toEqual({ |
| 170 | + ngModule: NgRxAutoEntityFeatureModule, |
| 171 | + providers: expect.any(Array) |
| 172 | + }); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments