-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(registerMock): allow use of mocked generics in register mock fac…
…tory
- Loading branch information
Showing
3 changed files
with
93 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { NoTransformerError } from './errors/no-transformer.error'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export function registerMock<T extends object>(factory: () => T): void { | ||
export function registerMock<T extends object>(factory: (...args) => T): void { | ||
throw new Error(NoTransformerError); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { createMock, registerMock } from 'ts-auto-mock'; | ||
|
||
describe('registerMock of type with generics', () => { | ||
it('should provide mocked version of generics as parameters', () => { | ||
type GenericsType<T> = { | ||
internalProp: T; | ||
}; | ||
|
||
type GenericsInterface<T, K> = { | ||
internalProp: T; | ||
internalProp2: K; | ||
}; | ||
|
||
type GenericsInterface2<T, K> = { | ||
internalProp: T; | ||
internalProp2: K; | ||
}; | ||
|
||
interface AParentInterface { | ||
prop: GenericsType<string>; | ||
prop2: GenericsInterface<number, 'Test'>; | ||
prop3: GenericsInterface2<string, GenericsType<string>>; | ||
} | ||
|
||
registerMock<GenericsType<unknown>>((generic: unknown) => ({ | ||
internalProp: (generic as object).toString() + '-mocked', | ||
})); | ||
|
||
registerMock<GenericsInterface<unknown, unknown>>( | ||
(genericNumber: unknown, genericString: unknown) => ({ | ||
internalProp: (genericNumber as number) + 5, | ||
internalProp2: (genericString as object).toString() + '-mocked', | ||
}) | ||
); | ||
|
||
registerMock<GenericsInterface2<unknown, unknown>>( | ||
(genericString: unknown, genericObject: unknown) => { | ||
(genericObject as GenericsType<string>).internalProp += '-mocked'; | ||
return { | ||
internalProp: (genericString as object).toString() + '-mocked', | ||
internalProp2: genericObject, | ||
}; | ||
} | ||
); | ||
const mock: AParentInterface = createMock<AParentInterface>(); | ||
|
||
expect(mock.prop.internalProp).toBe('-mocked'); | ||
expect(mock.prop2.internalProp).toBe(5); | ||
expect(mock.prop2.internalProp2).toBe('Test-mocked'); | ||
expect(mock.prop3.internalProp).toBe('-mocked'); | ||
expect(mock.prop3.internalProp2).toEqual({ | ||
internalProp: '-mocked-mocked', | ||
}); | ||
}); | ||
}); |