Skip to content

Commit aa2ebad

Browse files
authored
feat(hydrate-mocks): add create-hydrated-mock functionality
* refactor(hydrate-mocks): remove duplicated code * refactor(hydrate-mocks): revert changes * feat(hydrate-mocks): red - add create-hydrated-mock type and transformation * feat(hydrate-mocks): green - add support for basic createHydratedMock * feat(hydrate-mocks): green - add support for union types * feat(hydrate-mocks): green - make sure undefined union types will result in undefined * feat(hydrate-mocks): green - refactor tests * feat(hydrate-mocks): green - remove unnecessary check * feat(hydrate-mocks): refactor - rename test file * feat(hydrate-mocks): refactor - rename test description * feat(hydrate-mocks): red - make sure createHydratedMock doesn't is independent from createMock * feat(hydrate-mocks): red - remove unnecessary check in the test * feat(hydrate-mocks): green - always returns a new mock when is hydrated * feat(hydrate-mocks): red - add test when creatingMock with createMock after a mock has been created with createHydratedMock * feat(hydrate-mocks): green - duplicate all the cache (declaration, factory and file registration) to support hydrated mocks * feat(hydrate-mocks): refactor - extract modulesNameIdentifierPerFile * feat(hydrate-mocks): green - add hydrated initial generic support * feat(hydrate-mocks): red - add hydrated tests for generic with extends * feat(hydrate-mocks): green - use the correct mock for generic extensions * feat(hydrate-mocks): green - run createHydratedMock on ci * feat(hydrate-mocks): green - add support for circular generic extensions * feat(hydrate-mocks): green - add support for register mock * feat(hydrate-mocks): green - add test for type of enum * feat(hydrate-mocks): green - add test to make sure createMock and createHydratedMocks are independent * feat(hydrate-mocks): green - add support for intersections * feat(hydrate-mocks): refactor - create a function to handle type of declaration based on scope * feat(hydrate-mocks): refactor - create clone static method on Scope so it will be easier to carry hydrated when creating a new scope * feat(hydrate-mocks): refactor - remove duplicated code in register mock * feat(hydrate-mocks): doc - add createHydratedMock documentation
1 parent 34fa458 commit aa2ebad

File tree

20 files changed

+667
-137
lines changed

20 files changed

+667
-137
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
"build:transformer:definitelyTyped": "webpack --config config/modules/definitelyTypedTransformer/webpack.functions.js && webpack --config config/modules/definitelyTypedTransformer/webpack.js",
1313
"build:transformer:definitelyTyped:debug": "cross-env DEBUG=true webpack --config config/modules/definitelyTypedTransformer/webpack.functions.js && cross-env DEBUG=true webpack --config config/modules/definitelyTypedTransformer/webpack.js",
1414
"build:playground": "ttsc --project ./test/playground/tsconfig.build.json",
15-
"test": "npm run test:transformer && npm run test:noTransformer && npm run test:framework:context && npm run test:framework && npm run test:frameworkDeprecated && npm run test:registerMock && npm run test:features && npm run test:filesFilter && npm run test:logs && npm run test:unit",
15+
"test": "npm run test:transformer && npm run test:noTransformer && npm run test:framework:context && npm run test:framework && npm run test:frameworkDeprecated && npm run test:registerMock && npm run test:createHydratedMock && npm run test:features && npm run test:filesFilter && npm run test:logs && npm run test:unit",
1616
"test:unit": "cross-env JASMINE_CONFIG=./test/unit/jasmine.json TSCONFIG=./test/tsconfig.json npm run test:common",
1717
"test:transformer": "cross-env JASMINE_CONFIG=./test/transformer/jasmine.json TSCONFIG=./test/transformer/tsconfig.json npm run test:common",
1818
"test:noTransformer": "cross-env JASMINE_CONFIG=./test/noTransformer/jasmine.json TSCONFIG=./test/tsconfig.json npm run test:common",
1919
"test:registerMock": "cross-env JASMINE_CONFIG=./test/registerMock/jasmine.json TSCONFIG=./test/registerMock/tsconfig.json npm run test:common",
20+
"test:createHydratedMock": "cross-env JASMINE_CONFIG=./test/createHydratedMock/jasmine.json TSCONFIG=./test/createHydratedMock/tsconfig.json npm run test:common",
2021
"test:framework:context": "cross-env JASMINE_CONFIG=./test/frameworkContext/jasmine.json TSCONFIG=./test/frameworkContext/tsconfig.json npm run test:common",
2122
"test:frameworkDeprecated": "cross-env JASMINE_CONFIG=./test/frameworkContext/jasmineDeprecated.json TSCONFIG=./test/frameworkContext/tsconfig.json npm run test:common",
2223
"test:framework": "cross-env JASMINE_CONFIG=./test/framework/jasmine.json TSCONFIG=./test/framework/tsconfig.json npm run test:common",

src/create-hydrated-mock.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NoTransformerError } from './errors/no-transformer.error';
2+
3+
import { PartialDeep } from './partial/partial';
4+
5+
export function createHydratedMock<T extends object>(
6+
_values?: PartialDeep<T>
7+
): T {
8+
throw new Error(NoTransformerError);
9+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { createMock } from './create-mock';
22
export { createMockList } from './create-mock-list';
3+
export { createHydratedMock } from './create-hydrated-mock';
34
export { registerMock } from './register-mock';

src/transformer/descriptor/mock/mockProperties.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function GetMockPropertiesFromDeclarations(
4444
return false;
4545
}
4646

47-
if (member.questionToken) {
47+
if (member.questionToken && !scope.hydrated) {
4848
return false;
4949
}
5050

src/transformer/descriptor/typeParameter/typeParameter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ export function GetTypeParameterDescriptor(
3333
);
3434
}
3535

36-
const genericKey: string = MockDefiner.instance.getDeclarationKeyMap(
37-
typeDeclaration
36+
const genericKey: string = MockDefiner.instance.getDeclarationKeyMapBasedOnScope(
37+
typeDeclaration,
38+
scope
3839
);
3940

4041
return createFunctionToAccessToGenericValue(

src/transformer/descriptor/typeQuery/typeQuery.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ export function GetTypeQueryDescriptorFromDeclaration(
8080
// TODO: Use following two lines when issue #17552 on typescript github is resolved (https://github.com/microsoft/TypeScript/issues/17552)
8181
// TheNewEmitResolver.ensureEmitOf(GetImportDeclarationOf(node.eprName as ts.Identifier);
8282
// return node.exprName as ts.Identifier;
83-
return GetMockFactoryCallTypeofEnum(declaration as ts.EnumDeclaration);
83+
return GetMockFactoryCallTypeofEnum(
84+
declaration as ts.EnumDeclaration,
85+
scope
86+
);
8487
case ts.SyntaxKind.FunctionDeclaration:
8588
case ts.SyntaxKind.MethodSignature:
8689
return GetMethodDeclarationDescriptor(

src/transformer/descriptor/typeReference/typeReference.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ export function GetTypeReferenceDescriptor(
2121
node.typeName
2222
);
2323

24-
if (MockDefiner.instance.hasMockForDeclaration(declaration)) {
25-
return GetMockFactoryCall(node, scope);
24+
if (MockDefiner.instance.hasMockForDeclaration(declaration, scope)) {
25+
return GetMockFactoryCall(node, declaration, scope);
2626
}
2727

2828
if (IsTypescriptType(declaration)) {
2929
return GetTypescriptTypeDescriptor(node, scope);
3030
}
3131

3232
if (isTypeReferenceReusable(declaration)) {
33-
return CreateMockFactory(node, scope);
33+
return CreateMockFactory(node, declaration, scope);
3434
}
3535

3636
return GetDescriptor(declaration, scope);

src/transformer/descriptor/union/union.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ export function GetUnionDescriptor(
1010
): ts.Expression {
1111
const findNodes: ts.Node[] = GetTypes(node.types, scope);
1212

13+
if (scope.hydrated) {
14+
const removeUndefinedNodes: ts.Node[] = findNodes.filter(
15+
(typeNode: ts.TypeNode) => !isNotDefinedType(typeNode)
16+
);
17+
18+
if (removeUndefinedNodes.length) {
19+
return GetDescriptor(removeUndefinedNodes[0], scope);
20+
}
21+
22+
return GetUndefinedDescriptor();
23+
}
24+
1325
const notDefinedType: ts.Node[] = findNodes.filter((typeNode: ts.TypeNode) =>
1426
isNotDefinedType(typeNode)
1527
);

src/transformer/genericDeclaration/genericDeclaration.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,13 @@ export function GenericDeclaration(scope: Scope): IGenericDeclaration {
132132
genericNode.typeName
133133
);
134134

135+
const typeParameterDeclarationKey: string = MockDefiner.instance.getDeclarationKeyMapBasedOnScope(
136+
typeParameterDeclaration,
137+
scope
138+
);
139+
135140
const isExtendingItself: boolean =
136-
MockDefiner.instance.getDeclarationKeyMap(
137-
typeParameterDeclaration
138-
) === declarationKey;
141+
typeParameterDeclarationKey === declarationKey;
139142
if (isExtendingItself) {
140143
// FIXME: Currently, circular generics aren't supported. See
141144
// https://github.com/Typescript-TDD/ts-auto-mock/pull/312 for more

src/transformer/mock/mock.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ function getMockExpression(nodeToMock: ts.TypeNode): ts.Expression {
1818
return GetDescriptor(nodeToMock, new Scope());
1919
}
2020

21+
function getMockHydratedExpression(nodeToMock: ts.TypeNode): ts.Expression {
22+
const scope: Scope = new Scope();
23+
scope.hydrated = true;
24+
return GetDescriptor(nodeToMock, scope);
25+
}
26+
2127
function hasDefaultValues(node: ts.CallExpression): boolean {
2228
return !!node.arguments.length && !!node.arguments[0];
2329
}
@@ -41,6 +47,21 @@ export function getMock(
4147
return mockExpression;
4248
}
4349

50+
export function getHydratedMock(
51+
nodeToMock: ts.TypeNode,
52+
node: ts.CallExpression
53+
): ts.Expression {
54+
SetCurrentCreateMock(node);
55+
56+
const mockExpression: ts.Expression = getMockHydratedExpression(nodeToMock);
57+
58+
if (hasDefaultValues(node)) {
59+
return getMockMergeExpression(mockExpression, node.arguments[0]);
60+
}
61+
62+
return mockExpression;
63+
}
64+
4465
export function getMockForList(
4566
nodeToMock: ts.TypeNode,
4667
node: ts.CallExpression

0 commit comments

Comments
 (0)