-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add global restore function to allow for single call import restorati…
…on (#21) * Add restore() onto ImportMock class * Add restore() to documentation * Update expected versions * 1.3.0
- Loading branch information
Showing
11 changed files
with
149 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,36 @@ | ||
import * as sinonModule from 'sinon'; | ||
import { MockManager, OtherManager, StaticMockManager } from './managers/index'; | ||
import { IConstruct, IModule } from './types'; | ||
import { IConstruct, IModule, IManager } from './types'; | ||
const sinon = sinonModule as sinonModule.SinonStatic; | ||
|
||
export class ImportMock { | ||
private static sandboxedItems: IManager[] = []; | ||
|
||
private static sandbox<T extends IManager>(mock: T): T { | ||
ImportMock.sandboxedItems.push(mock); | ||
return mock; | ||
} | ||
|
||
public static mockClass<T, K extends IModule = any>( | ||
module: { [importName: string]: IConstruct<T> } | K, importName: keyof K = 'default'): MockManager<T> { | ||
return new MockManager<T>(module, importName as string); | ||
return ImportMock.sandbox(new MockManager<T>(module, importName as string)); | ||
} | ||
|
||
public static mockStaticClass<T, K extends IModule = any>( | ||
module: { [importName: string]: IConstruct<T> } | K, importName: keyof K = 'default'): StaticMockManager<T> { | ||
return new StaticMockManager<T>(module, importName as string); | ||
return ImportMock.sandbox(new StaticMockManager<T>(module, importName as string)); | ||
} | ||
|
||
public static mockFunction<K extends IModule>(module: { [importName: string]: () => any } | K, importName: keyof K = 'default', returns?: any): sinon.SinonStub { | ||
return sinon.stub(module, importName as string).returns(returns); | ||
return ImportMock.sandbox(sinon.stub(module, importName as string).returns(returns)); | ||
} | ||
|
||
public static mockOther<T extends IModule, K extends keyof T>(module: { [importName: string]: T[K] } | T, importName?: K, replaceWith?: Partial<T[K]>): OtherManager<T[K]> { | ||
return ImportMock.sandbox(new OtherManager<T[K]>(module, importName as string || 'default', replaceWith)); | ||
} | ||
|
||
public static mockOther<T extends IModule, K extends keyof T>(module: { [importName: string]: T[K] } | T, importName?: K, replaceWith?: Partial<T[K]>) { | ||
return new OtherManager<T[K]>(module, importName as string || 'default', replaceWith); | ||
public static restore(): void { | ||
ImportMock.sandboxedItems.forEach(item => item.restore()); | ||
ImportMock.sandboxedItems = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './construct'; | ||
export * from './manager'; | ||
export * from './module'; | ||
export * from './stringKeyOf'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface IManager { | ||
restore: () => void | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './default-class-consumer'; | ||
export * from './static-test-class-consumer'; | ||
export * from './test-class-consumer'; | ||
export * from './function-consumer'; | ||
export * from './other-consumer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.