Closed Darkein closed 6 years ago
Hi @Darkein, sure you can. You just have to define a strategyStub overriding the SessionStorageStrategy like i do here for the localStorage.
The strategy indexer will take the last strategy defined for a given type (in your case session_storage)
You can define a stub class implementing the StorageStrategy interface or use the StorageStrategyStub that i m exposing as helper.
StorageStrategyStub is a class for test only purpose. It allow to define the strategyName at instantiation. By using an existing name and declaring the new strategy with the multi provider you can overriding the existing one.
Regards
Edit :
Don't forget to call StrategyIndex.clear() after each tests (example). If you don't, the indexer will skip the strategy indexing the next time you configure the testingModule, resulting that the new strategies that you could declare will not override existing ones.
Hello @PillowPillow , thanks for your answers.
Unfortunately It doesn't work. I tried to debug my tests and it seems NgxWebstorageModule.forRoot()
try to instanciate LocalStorageStrategy
which try to inject LOCAL_STORAGE
, even if I don't use them. I will try to mock them before mocking sessionStorageStrategy.
I keep you in touch :)
mmh interesting, I m not using jest but it shouldn't throw any error with LOCAL_STORAGE & SESSION_STORAGE. Moreover i m checking if window exists before doing anything in order to handle server side usage.
Can you show me the complete stacktrace ? Or even better provide me an ultra basic repo that i could use to debug that?
Hum, I can't mock LOCAL_STORAGE because it's injected in the import. I will not have time to create a repo right now, but I can send you the (horrible) stack trace.
ReferenceError: Storage is not defined
at LocalStorageStrategy.ctorParameters (**\node_modules\ngx-webstorage\bundles\ng:\ngx-webstorage\lib\strategies\localStorage.ts:12:56)
and my test file:
describe('FormEditService', () => {
beforeEach(() => {
const strategyStub: StorageStrategy<any> = new StorageStrategyStub(SessionStorageStrategy.strategyName);
const formServiceStub = { save: () => of({}) };
const routerStub = { navigate: () => {} };
TestBed.configureTestingModule({
imports: [NgxWebstorageModule.forRoot()],
providers: [
FormEditService,
{ provide: LOCAL_STORAGE, useValue: () => {} },
{ provide: SESSION_STORAGE, useValue: () => {} },
{ provide: STORAGE_STRATEGIES, useFactory: () => strategyStub, multi: true },
{ provide: FormService, useValue: formServiceStub },
{ provide: Router, useValue: routerStub }
]
});
});
afterEach(() => StrategyIndex.clear());
//...
})
mmh, which version of typescript are you using? As far i can see, the error come from the type that i'm using in the strategy's construtor - Storage - which come from the typescript's lib.dom file. I can try to remove its usages and deploy a new version.
I deployed a new version 3.0.0-beta.12 removing the typescript's Storage interface usage. Can you remove the LOCAL_STORAGE & SESSION_STORAGE overriding from your test file and tell me if it works?
Hum, I can't mock LOCAL_STORAGE because it's injected in the import.
The providers defined in the current module always have the priority over the library ones.
Wow, well played, it's working !
Thanks a lot :)
PS: I'm using typescript 3.1.3.
Hello,
I'm using sesssionStorage decorator inside a service which I want to test.
Because i'm using jest for testing, I don't have access to SessionStorage. Can I mock the sessionStorage to use InMemoryStorage instead?
I tried to inspire myself with your tests, but nothing worked ( I always have
ReferenceError: Storage is not defined
)Thanks for your help