From 3d4488b95dfd6f8371f59ede06aa8bd73ad9be38 Mon Sep 17 00:00:00 2001 From: MG Date: Sun, 7 Jun 2020 12:26:49 +0200 Subject: [PATCH] test: unite jasmine and jest tests --- Dockerfile | 2 +- README.md | 17 +- .../MockBuilder/fixtures.components.ts | 159 ---------- .../MockComponent/dependency.component.ts | 18 -- .../MockModule/dependency.component.ts | 18 -- .../MockRender/MockRender.spec.ts | 64 ---- .../MockRender/dependency.component.ts | 18 -- .../NG_MOCKS/fixtures.components.ts | 110 ------- examples-jest/MockBuilder/MockBuilder.spec.ts | 223 -------------- .../MockBuilder/fixtures.directives.ts | 16 - examples-jest/MockBuilder/fixtures.modules.ts | 88 ------ examples-jest/MockBuilder/fixtures.pipes.ts | 56 ---- .../MockBuilder/fixtures.services.ts | 100 ------ examples-jest/MockBuilder/fixtures.tokens.ts | 7 - .../MockComponent/MockComponent.spec.ts | 86 ------ .../MockComponent/tested.component.ts | 12 - .../MockDirective.spec.ts | 45 --- .../dependency.directive.ts | 12 - .../tested.component.ts | 10 - .../MockDirective.spec.ts | 44 --- .../dependency.directive.ts | 12 - .../tested.component.ts | 10 - examples-jest/MockModule/MockModule.spec.ts | 25 -- examples-jest/MockModule/dependency.module.ts | 10 - examples-jest/MockModule/tested.component.ts | 12 - examples-jest/MockPipe/MockPipe.spec.ts | 30 -- examples-jest/MockPipe/dependency.pipe.ts | 6 - examples-jest/MockPipe/tested.component.ts | 7 - .../MockReactiveForms.spec.ts | 36 --- .../MockReactiveForms/dependency.component.ts | 7 - .../MockReactiveForms/tested.component.ts | 10 - examples-jest/MockRender/dependency.module.ts | 9 - examples-jest/MockRender/tested.component.ts | 18 -- examples-jest/NG_MOCKS/NG_MOCKS.spec.ts | 122 -------- examples-jest/NG_MOCKS/fixtures.directives.ts | 16 - examples-jest/NG_MOCKS/fixtures.modules.ts | 88 ------ examples-jest/NG_MOCKS/fixtures.pipes.ts | 56 ---- examples-jest/NG_MOCKS/fixtures.services.ts | 100 ------ examples-jest/NG_MOCKS/fixtures.tokens.ts | 7 - .../MockBuilder/MockBuilder.spec.ts | 0 .../MockBuilder/fixtures.components.ts | 2 +- .../MockBuilder/fixtures.directives.ts | 0 .../MockBuilder/fixtures.modules.ts | 0 .../MockBuilder/fixtures.pipes.ts | 0 .../MockBuilder/fixtures.services.ts | 0 .../MockBuilder/fixtures.tokens.ts | 0 .../MockComponent/MockComponent.spec.ts | 0 .../MockComponent}/dependency.component.ts | 2 +- .../MockComponent/tested.component.ts | 0 .../MockDirective.spec.ts | 0 .../dependency.directive.ts | 0 .../tested.component.ts | 0 .../MockDirective.spec.ts | 0 .../dependency.directive.ts | 0 .../tested.component.ts | 0 .../MockModule/MockModule.spec.ts | 0 .../MockModule}/dependency.component.ts | 2 +- .../MockModule/dependency.module.ts | 0 .../MockModule/tested.component.ts | 0 .../MockPipe/MockPipe.spec.ts | 0 .../MockPipe/dependency.pipe.ts | 0 .../MockPipe/tested.component.ts | 0 .../MockReactiveForms.spec.ts | 0 .../MockReactiveForms/dependency.component.ts | 0 .../MockReactiveForms/tested.component.ts | 0 .../MockRender/MockRender.spec.ts | 8 +- .../MockRender}/dependency.component.ts | 2 +- .../MockRender/dependency.module.ts | 0 .../MockRender/tested.component.ts | 0 .../NG_MOCKS/NG_MOCKS.spec.ts | 0 .../NG_MOCKS/fixtures.components.ts | 2 +- .../NG_MOCKS/fixtures.directives.ts | 0 .../NG_MOCKS/fixtures.modules.ts | 0 .../NG_MOCKS/fixtures.pipes.ts | 0 .../NG_MOCKS/fixtures.services.ts | 0 .../NG_MOCKS/fixtures.tokens.ts | 0 karma.conf.ts | 6 +- lib/mock-component/mock-component.spec.ts | 2 +- .../template-outlet.component.ts | 2 +- lib/mock-directive/mock-directive.spec.ts | 2 +- package.json | 22 +- .../context-with-directives.spec.ts | 110 ------- .../test.spec.ts | 107 ------- tests-jasmine/exports-only/test.spec.ts | 64 ---- .../injected-ng-templates.spec.ts | 82 ----- .../internal-only-nested/test.spec.ts | 37 --- tests-jasmine/internal-only/test.spec.ts | 36 --- .../test.spec.ts | 44 --- tests-jasmine/on-push/on-push.spec.ts | 95 ------ tests-jasmine/spies/test.spec.ts | 70 ----- .../structural-directives.spec.ts | 287 ------------------ .../custom-root.component.ts | 41 --- .../custom-type.directive.ts | 13 - .../fixtures.ts | 61 ---- .../fixtures.ts | 70 ----- .../test.spec.ts | 129 -------- .../exports-only/fixtures.components.ts | 7 - tests-jest/exports-only/fixtures.modules.ts | 16 - tests-jest/get-inputs-and-outputs/fixtures.ts | 33 -- .../get-inputs-and-outputs/test.spec.ts | 75 ----- tests-jest/index.ts | 7 - .../custom-injection.component.ts | 18 -- .../fixtures.components.ts | 7 - .../internal-only-nested/fixtures.modules.ts | 25 -- .../internal-only/fixtures.components.ts | 7 - tests-jest/internal-only/fixtures.modules.ts | 10 - .../fixtures.components.ts | 13 - .../internal-vs-external/fixtures.modules.ts | 11 - tests-jest/internal-vs-external/test.spec.ts | 70 ----- .../fixtures.components.ts | 7 - .../fixtures.modules.ts | 9 - .../mock-builder-by-directive/test.spec.ts | 39 --- .../fixtures.components.ts | 7 - .../fixtures.modules.ts | 26 -- .../test.spec.ts | 40 --- .../module-with-factory-tokens/fixtures.ts | 28 -- .../module-with-factory-tokens/test.spec.ts | 111 ------- tests-jest/module-with-tokens/fixtures.ts | 40 --- tests-jest/module-with-tokens/test.spec.ts | 97 ------ .../nested-before-each/fixtures.components.ts | 7 - .../nested-before-each/fixtures.modules.ts | 9 - tests-jest/nested-before-each/test.spec.ts | 78 ----- .../fixtures.components.ts | 27 -- .../fixtures.modules.ts | 18 -- .../fixtures.services.ts | 10 - tests-jest/on-push/on-push.spec.ts | 95 ------ .../fixtures.components.ts | 15 - .../fixtures.modules.ts | 19 -- .../fixtures.services.ts | 24 -- .../provider-with-dependency/test.spec.ts | 69 ----- .../fixtures.components.ts | 11 - .../fixtures.module.ts | 11 - .../test.spec.ts | 42 --- .../fixtures.components.ts | 25 -- .../shared-mocked-module/fixtures.modules.ts | 31 -- tests-jest/shared-mocked-module/test.spec.ts | 44 --- tests-jest/spies/fixtures.components.ts | 20 -- tests-jest/spies/fixtures.modules.ts | 8 - tests-jest/spies/fixtures.providers.ts | 10 - .../custom-ng-for-with-of.directive.ts | 34 --- .../custom-ng-for-without-of.directive.ts | 34 --- .../custom-ng-if.directive.ts | 22 -- .../context-with-directives.spec.ts | 20 +- .../custom-root.component.ts | 0 .../custom-type.directive.ts | 0 .../fixtures.ts | 0 .../test.spec.ts | 10 +- .../fixtures.ts | 0 .../test.spec.ts | 14 +- .../exports-only/fixtures.components.ts | 0 .../exports-only/fixtures.modules.ts | 0 .../exports-only/test.spec.ts | 8 +- .../get-inputs-and-outputs/fixtures.ts | 0 .../get-inputs-and-outputs/test.spec.ts | 56 +++- {tests-jasmine => tests}/index.ts | 0 .../custom-injection.component.ts | 0 .../injected-ng-templates.spec.ts | 15 +- .../fixtures.components.ts | 0 .../internal-only-nested/fixtures.modules.ts | 0 .../internal-only-nested/test.spec.ts | 7 +- .../internal-only/fixtures.components.ts | 0 .../internal-only/fixtures.modules.ts | 0 .../internal-only/test.spec.ts | 7 +- .../fixtures.components.ts | 0 .../internal-vs-external/fixtures.modules.ts | 0 .../internal-vs-external/test.spec.ts | 16 +- .../fixtures.components.ts | 0 .../fixtures.modules.ts | 0 .../mock-builder-by-directive/test.spec.ts | 8 +- .../fixtures.components.ts | 0 .../fixtures.modules.ts | 0 .../test.spec.ts | 6 +- .../module-with-factory-tokens/fixtures.ts | 0 .../module-with-factory-tokens/test.spec.ts | 0 .../module-with-tokens/fixtures.ts | 0 .../module-with-tokens/test.spec.ts | 0 .../nested-before-each/fixtures.components.ts | 0 .../nested-before-each/fixtures.modules.ts | 0 .../nested-before-each/test.spec.ts | 12 +- .../fixtures.components.ts | 0 .../fixtures.modules.ts | 0 .../fixtures.services.ts | 0 .../test.spec.ts | 19 +- tests/on-push/on-push.spec.ts | 77 +++++ .../fixtures.components.ts | 0 .../fixtures.modules.ts | 0 .../fixtures.services.ts | 0 .../provider-with-dependency/test.spec.ts | 0 .../fixtures.components.ts | 0 .../fixtures.module.ts | 0 .../test.spec.ts | 0 .../fixtures.components.ts | 0 .../shared-mocked-module/fixtures.modules.ts | 0 .../shared-mocked-module/test.spec.ts | 8 +- .../spies/fixtures.components.ts | 0 .../spies/fixtures.modules.ts | 0 .../spies/fixtures.providers.ts | 0 {tests-jest => tests}/spies/test.spec.ts | 49 ++- .../custom-ng-for-with-of.directive.ts | 0 .../custom-ng-for-without-of.directive.ts | 0 .../custom-ng-if.directive.ts | 0 .../structural-directives.spec.ts | 23 +- 202 files changed, 217 insertions(+), 4488 deletions(-) delete mode 100644 examples-jasmine/MockBuilder/fixtures.components.ts delete mode 100644 examples-jasmine/MockComponent/dependency.component.ts delete mode 100644 examples-jasmine/MockModule/dependency.component.ts delete mode 100644 examples-jasmine/MockRender/MockRender.spec.ts delete mode 100644 examples-jasmine/MockRender/dependency.component.ts delete mode 100644 examples-jasmine/NG_MOCKS/fixtures.components.ts delete mode 100644 examples-jest/MockBuilder/MockBuilder.spec.ts delete mode 100644 examples-jest/MockBuilder/fixtures.directives.ts delete mode 100644 examples-jest/MockBuilder/fixtures.modules.ts delete mode 100644 examples-jest/MockBuilder/fixtures.pipes.ts delete mode 100644 examples-jest/MockBuilder/fixtures.services.ts delete mode 100644 examples-jest/MockBuilder/fixtures.tokens.ts delete mode 100644 examples-jest/MockComponent/MockComponent.spec.ts delete mode 100644 examples-jest/MockComponent/tested.component.ts delete mode 100644 examples-jest/MockDirective-Attribute/MockDirective.spec.ts delete mode 100644 examples-jest/MockDirective-Attribute/dependency.directive.ts delete mode 100644 examples-jest/MockDirective-Attribute/tested.component.ts delete mode 100644 examples-jest/MockDirective-Structural/MockDirective.spec.ts delete mode 100644 examples-jest/MockDirective-Structural/dependency.directive.ts delete mode 100644 examples-jest/MockDirective-Structural/tested.component.ts delete mode 100644 examples-jest/MockModule/MockModule.spec.ts delete mode 100644 examples-jest/MockModule/dependency.module.ts delete mode 100644 examples-jest/MockModule/tested.component.ts delete mode 100644 examples-jest/MockPipe/MockPipe.spec.ts delete mode 100644 examples-jest/MockPipe/dependency.pipe.ts delete mode 100644 examples-jest/MockPipe/tested.component.ts delete mode 100644 examples-jest/MockReactiveForms/MockReactiveForms.spec.ts delete mode 100644 examples-jest/MockReactiveForms/dependency.component.ts delete mode 100644 examples-jest/MockReactiveForms/tested.component.ts delete mode 100644 examples-jest/MockRender/dependency.module.ts delete mode 100644 examples-jest/MockRender/tested.component.ts delete mode 100644 examples-jest/NG_MOCKS/NG_MOCKS.spec.ts delete mode 100644 examples-jest/NG_MOCKS/fixtures.directives.ts delete mode 100644 examples-jest/NG_MOCKS/fixtures.modules.ts delete mode 100644 examples-jest/NG_MOCKS/fixtures.pipes.ts delete mode 100644 examples-jest/NG_MOCKS/fixtures.services.ts delete mode 100644 examples-jest/NG_MOCKS/fixtures.tokens.ts rename {examples-jasmine => examples}/MockBuilder/MockBuilder.spec.ts (100%) rename {examples-jest => examples}/MockBuilder/fixtures.components.ts (99%) rename {examples-jasmine => examples}/MockBuilder/fixtures.directives.ts (100%) rename {examples-jasmine => examples}/MockBuilder/fixtures.modules.ts (100%) rename {examples-jasmine => examples}/MockBuilder/fixtures.pipes.ts (100%) rename {examples-jasmine => examples}/MockBuilder/fixtures.services.ts (100%) rename {examples-jasmine => examples}/MockBuilder/fixtures.tokens.ts (100%) rename {examples-jasmine => examples}/MockComponent/MockComponent.spec.ts (100%) rename {examples-jest/MockModule => examples/MockComponent}/dependency.component.ts (89%) rename {examples-jasmine => examples}/MockComponent/tested.component.ts (100%) rename {examples-jasmine => examples}/MockDirective-Attribute/MockDirective.spec.ts (100%) rename {examples-jasmine => examples}/MockDirective-Attribute/dependency.directive.ts (100%) rename {examples-jasmine => examples}/MockDirective-Attribute/tested.component.ts (100%) rename {examples-jasmine => examples}/MockDirective-Structural/MockDirective.spec.ts (100%) rename {examples-jasmine => examples}/MockDirective-Structural/dependency.directive.ts (100%) rename {examples-jasmine => examples}/MockDirective-Structural/tested.component.ts (100%) rename {examples-jasmine => examples}/MockModule/MockModule.spec.ts (100%) rename {examples-jest/MockRender => examples/MockModule}/dependency.component.ts (89%) rename {examples-jasmine => examples}/MockModule/dependency.module.ts (100%) rename {examples-jasmine => examples}/MockModule/tested.component.ts (100%) rename {examples-jasmine => examples}/MockPipe/MockPipe.spec.ts (100%) rename {examples-jasmine => examples}/MockPipe/dependency.pipe.ts (100%) rename {examples-jasmine => examples}/MockPipe/tested.component.ts (100%) rename {examples-jasmine => examples}/MockReactiveForms/MockReactiveForms.spec.ts (100%) rename {examples-jasmine => examples}/MockReactiveForms/dependency.component.ts (100%) rename {examples-jasmine => examples}/MockReactiveForms/tested.component.ts (100%) rename {examples-jest => examples}/MockRender/MockRender.spec.ts (89%) rename {examples-jest/MockComponent => examples/MockRender}/dependency.component.ts (89%) rename {examples-jasmine => examples}/MockRender/dependency.module.ts (100%) rename {examples-jasmine => examples}/MockRender/tested.component.ts (100%) rename {examples-jasmine => examples}/NG_MOCKS/NG_MOCKS.spec.ts (100%) rename {examples-jest => examples}/NG_MOCKS/fixtures.components.ts (98%) rename {examples-jasmine => examples}/NG_MOCKS/fixtures.directives.ts (100%) rename {examples-jasmine => examples}/NG_MOCKS/fixtures.modules.ts (100%) rename {examples-jasmine => examples}/NG_MOCKS/fixtures.pipes.ts (100%) rename {examples-jasmine => examples}/NG_MOCKS/fixtures.services.ts (100%) rename {examples-jasmine => examples}/NG_MOCKS/fixtures.tokens.ts (100%) delete mode 100644 tests-jasmine/context-with-directives/context-with-directives.spec.ts delete mode 100644 tests-jasmine/control-value-accessor-form-control/test.spec.ts delete mode 100644 tests-jasmine/exports-only/test.spec.ts delete mode 100644 tests-jasmine/injected-ng-templates/injected-ng-templates.spec.ts delete mode 100644 tests-jasmine/internal-only-nested/test.spec.ts delete mode 100644 tests-jasmine/internal-only/test.spec.ts delete mode 100644 tests-jasmine/normal-usage-after-mock-builder/test.spec.ts delete mode 100644 tests-jasmine/on-push/on-push.spec.ts delete mode 100644 tests-jasmine/spies/test.spec.ts delete mode 100644 tests-jasmine/structural-directives/structural-directives.spec.ts delete mode 100644 tests-jest/context-with-directives/custom-root.component.ts delete mode 100644 tests-jest/context-with-directives/custom-type.directive.ts delete mode 100644 tests-jest/control-value-accessor-form-control/fixtures.ts delete mode 100644 tests-jest/control-value-accessor-ng-model/fixtures.ts delete mode 100644 tests-jest/control-value-accessor-ng-model/test.spec.ts delete mode 100644 tests-jest/exports-only/fixtures.components.ts delete mode 100644 tests-jest/exports-only/fixtures.modules.ts delete mode 100644 tests-jest/get-inputs-and-outputs/fixtures.ts delete mode 100644 tests-jest/get-inputs-and-outputs/test.spec.ts delete mode 100644 tests-jest/index.ts delete mode 100644 tests-jest/injected-ng-templates/custom-injection.component.ts delete mode 100644 tests-jest/internal-only-nested/fixtures.components.ts delete mode 100644 tests-jest/internal-only-nested/fixtures.modules.ts delete mode 100644 tests-jest/internal-only/fixtures.components.ts delete mode 100644 tests-jest/internal-only/fixtures.modules.ts delete mode 100644 tests-jest/internal-vs-external/fixtures.components.ts delete mode 100644 tests-jest/internal-vs-external/fixtures.modules.ts delete mode 100644 tests-jest/internal-vs-external/test.spec.ts delete mode 100644 tests-jest/mock-builder-by-directive/fixtures.components.ts delete mode 100644 tests-jest/mock-builder-by-directive/fixtures.modules.ts delete mode 100644 tests-jest/mock-builder-by-directive/test.spec.ts delete mode 100644 tests-jest/mock-builder-keeps-application-module/fixtures.components.ts delete mode 100644 tests-jest/mock-builder-keeps-application-module/fixtures.modules.ts delete mode 100644 tests-jest/mock-builder-keeps-application-module/test.spec.ts delete mode 100644 tests-jest/module-with-factory-tokens/fixtures.ts delete mode 100644 tests-jest/module-with-factory-tokens/test.spec.ts delete mode 100644 tests-jest/module-with-tokens/fixtures.ts delete mode 100644 tests-jest/module-with-tokens/test.spec.ts delete mode 100644 tests-jest/nested-before-each/fixtures.components.ts delete mode 100644 tests-jest/nested-before-each/fixtures.modules.ts delete mode 100644 tests-jest/nested-before-each/test.spec.ts delete mode 100644 tests-jest/normal-usage-after-mock-builder/fixtures.components.ts delete mode 100644 tests-jest/normal-usage-after-mock-builder/fixtures.modules.ts delete mode 100644 tests-jest/normal-usage-after-mock-builder/fixtures.services.ts delete mode 100644 tests-jest/on-push/on-push.spec.ts delete mode 100644 tests-jest/provider-with-dependency/fixtures.components.ts delete mode 100644 tests-jest/provider-with-dependency/fixtures.modules.ts delete mode 100644 tests-jest/provider-with-dependency/fixtures.services.ts delete mode 100644 tests-jest/provider-with-dependency/test.spec.ts delete mode 100644 tests-jest/rerender-rendered-content-child/fixtures.components.ts delete mode 100644 tests-jest/rerender-rendered-content-child/fixtures.module.ts delete mode 100644 tests-jest/rerender-rendered-content-child/test.spec.ts delete mode 100644 tests-jest/shared-mocked-module/fixtures.components.ts delete mode 100644 tests-jest/shared-mocked-module/fixtures.modules.ts delete mode 100644 tests-jest/shared-mocked-module/test.spec.ts delete mode 100644 tests-jest/spies/fixtures.components.ts delete mode 100644 tests-jest/spies/fixtures.modules.ts delete mode 100644 tests-jest/spies/fixtures.providers.ts delete mode 100644 tests-jest/structural-directives/custom-ng-for-with-of.directive.ts delete mode 100644 tests-jest/structural-directives/custom-ng-for-without-of.directive.ts delete mode 100644 tests-jest/structural-directives/custom-ng-if.directive.ts rename {tests-jest => tests}/context-with-directives/context-with-directives.spec.ts (86%) rename {tests-jasmine => tests}/context-with-directives/custom-root.component.ts (100%) rename {tests-jasmine => tests}/context-with-directives/custom-type.directive.ts (100%) rename {tests-jasmine => tests}/control-value-accessor-form-control/fixtures.ts (100%) rename {tests-jest => tests}/control-value-accessor-form-control/test.spec.ts (90%) rename {tests-jasmine => tests}/control-value-accessor-ng-model/fixtures.ts (100%) rename {tests-jasmine => tests}/control-value-accessor-ng-model/test.spec.ts (88%) rename {tests-jasmine => tests}/exports-only/fixtures.components.ts (100%) rename {tests-jasmine => tests}/exports-only/fixtures.modules.ts (100%) rename {tests-jest => tests}/exports-only/test.spec.ts (90%) rename {tests-jasmine => tests}/get-inputs-and-outputs/fixtures.ts (100%) rename {tests-jasmine => tests}/get-inputs-and-outputs/test.spec.ts (62%) rename {tests-jasmine => tests}/index.ts (100%) rename {tests-jasmine => tests}/injected-ng-templates/custom-injection.component.ts (100%) rename {tests-jest => tests}/injected-ng-templates/injected-ng-templates.spec.ts (87%) rename {tests-jasmine => tests}/internal-only-nested/fixtures.components.ts (100%) rename {tests-jasmine => tests}/internal-only-nested/fixtures.modules.ts (100%) rename {tests-jest => tests}/internal-only-nested/test.spec.ts (88%) rename {tests-jasmine => tests}/internal-only/fixtures.components.ts (100%) rename {tests-jasmine => tests}/internal-only/fixtures.modules.ts (100%) rename {tests-jest => tests}/internal-only/test.spec.ts (87%) rename {tests-jasmine => tests}/internal-vs-external/fixtures.components.ts (100%) rename {tests-jasmine => tests}/internal-vs-external/fixtures.modules.ts (100%) rename {tests-jasmine => tests}/internal-vs-external/test.spec.ts (84%) rename {tests-jasmine => tests}/mock-builder-by-directive/fixtures.components.ts (100%) rename {tests-jasmine => tests}/mock-builder-by-directive/fixtures.modules.ts (100%) rename {tests-jasmine => tests}/mock-builder-by-directive/test.spec.ts (83%) rename {tests-jasmine => tests}/mock-builder-keeps-application-module/fixtures.components.ts (100%) rename {tests-jasmine => tests}/mock-builder-keeps-application-module/fixtures.modules.ts (100%) rename {tests-jasmine => tests}/mock-builder-keeps-application-module/test.spec.ts (92%) rename {tests-jasmine => tests}/module-with-factory-tokens/fixtures.ts (100%) rename {tests-jasmine => tests}/module-with-factory-tokens/test.spec.ts (100%) rename {tests-jasmine => tests}/module-with-tokens/fixtures.ts (100%) rename {tests-jasmine => tests}/module-with-tokens/test.spec.ts (100%) rename {tests-jasmine => tests}/nested-before-each/fixtures.components.ts (100%) rename {tests-jasmine => tests}/nested-before-each/fixtures.modules.ts (100%) rename {tests-jasmine => tests}/nested-before-each/test.spec.ts (85%) rename {tests-jasmine => tests}/normal-usage-after-mock-builder/fixtures.components.ts (100%) rename {tests-jasmine => tests}/normal-usage-after-mock-builder/fixtures.modules.ts (100%) rename {tests-jasmine => tests}/normal-usage-after-mock-builder/fixtures.services.ts (100%) rename {tests-jest => tests}/normal-usage-after-mock-builder/test.spec.ts (69%) create mode 100644 tests/on-push/on-push.spec.ts rename {tests-jasmine => tests}/provider-with-dependency/fixtures.components.ts (100%) rename {tests-jasmine => tests}/provider-with-dependency/fixtures.modules.ts (100%) rename {tests-jasmine => tests}/provider-with-dependency/fixtures.services.ts (100%) rename {tests-jasmine => tests}/provider-with-dependency/test.spec.ts (100%) rename {tests-jasmine => tests}/rerender-rendered-content-child/fixtures.components.ts (100%) rename {tests-jasmine => tests}/rerender-rendered-content-child/fixtures.module.ts (100%) rename {tests-jasmine => tests}/rerender-rendered-content-child/test.spec.ts (100%) rename {tests-jasmine => tests}/shared-mocked-module/fixtures.components.ts (100%) rename {tests-jasmine => tests}/shared-mocked-module/fixtures.modules.ts (100%) rename {tests-jasmine => tests}/shared-mocked-module/test.spec.ts (88%) rename {tests-jasmine => tests}/spies/fixtures.components.ts (100%) rename {tests-jasmine => tests}/spies/fixtures.modules.ts (100%) rename {tests-jasmine => tests}/spies/fixtures.providers.ts (100%) rename {tests-jest => tests}/spies/test.spec.ts (63%) rename {tests-jasmine => tests}/structural-directives/custom-ng-for-with-of.directive.ts (100%) rename {tests-jasmine => tests}/structural-directives/custom-ng-for-without-of.directive.ts (100%) rename {tests-jasmine => tests}/structural-directives/custom-ng-if.directive.ts (100%) rename {tests-jest => tests}/structural-directives/structural-directives.spec.ts (94%) diff --git a/Dockerfile b/Dockerfile index d79a70f9fe..518b7d1c0a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM satantime/puppeteer-node:10 +FROM satantime/puppeteer-node:10-buster-slim WORKDIR /app ENTRYPOINT ["/docker-entrypoint.sh"] diff --git a/README.md b/README.md index b681012b52..9e8e02f91a 100644 --- a/README.md +++ b/README.md @@ -20,28 +20,25 @@ Or you could use this to mock them out and have the ability to assert on their i For an easy start check the [MockBuilder](#mockbuilder) first. -- [jasmine examples](https://github.com/ike18t/ng-mocks/tree/master/examples-jasmine) -- [jest examples](https://github.com/ike18t/ng-mocks/tree/master/examples-jest) +- [examples from the doc](https://github.com/ike18t/ng-mocks/tree/master/examples) -* [jasmine e2e tests](https://github.com/ike18t/ng-mocks/tree/master/tests-jasmine) -* [jest e2e tests](https://github.com/ike18t/ng-mocks/tree/master/tests-jest) +* [current e2e tests](https://github.com/ike18t/ng-mocks/tree/master/tests) ### Sections: -- [MockModule](#mockmodule) - [MockComponent](#mockcomponents) - [MockDirective](#mockdirectives) - [MockPipe](#mockpipes) - [MockDeclaration](#mockdeclarations) +- [MockModule](#mockmodule) * [MockBuilder](#mockbuilder) - facilitate creation of a mocked environment * [MockRender](#mockrender) - facilitate render of components -* [MockHelper](#mockhelper) - facilitate extraction of directives of an element +* [ngMocks](#ngmocks) - facilitate extraction of directives of an element - [Reactive Forms Components](#mocked-reactive-forms-components) - [Structural Components](#usage-example-of-structural-directives) - [Auto Spy](#auto-spy) -- [More examples](#other-examples-of-tests) --- @@ -706,6 +703,9 @@ ngMocks provides functions to get attribute and structural directives from an el - ngMocks.flushTestBed() +
Click to see a usage example +

+ ```typescript // returns attribute or structural directive // which belongs to current element. @@ -796,6 +796,9 @@ describe('MockService', () => { }); ``` +

+
+ ## Auto Spy Add the next code to `src/test.ts` if you want all mocked methods and functions to be a jasmine spy. diff --git a/examples-jasmine/MockBuilder/fixtures.components.ts b/examples-jasmine/MockBuilder/fixtures.components.ts deleted file mode 100644 index ac437e30d9..0000000000 --- a/examples-jasmine/MockBuilder/fixtures.components.ts +++ /dev/null @@ -1,159 +0,0 @@ -import { Component, ContentChild, Inject, Input, Optional, TemplateRef } from '@angular/core'; - -import { staticFalse } from '../../tests-jasmine'; - -import { - AnythingWeWant1, - AnythingWeWant2, - MyCustomProvider1, - MyCustomProvider2, - MyCustomProvider3, - MyService1, - MyService2, - ServiceWeDontWantToMock, - ServiceWeWantToCustomize, - ServiceWeWantToMock, -} from './fixtures.services'; -import { - INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK, - INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE, - INJECTION_TOKEN_WE_WANT_TO_MOCK, -} from './fixtures.tokens'; - -@Component({ - selector: 'component-structural', - template: ` -
- -
- `, -}) -export class ComponentContentChild { - @ContentChild('block', { ...staticFalse }) injectedBlock: TemplateRef; - @Input() items?: T[]; -} - -@Component({ - selector: 'my-component', - template: ` -
My Content
- -
MyComponent1:
-
MyComponent2:
-
MyComponent3:
-
ComponentWeDontWantToMock:
-
ComponentWeWantToMock:
- -
MyDirective:
-
DirectiveWeDontWantToMock:
-
- DirectiveWeWantToMock 1: render {{ z.b }} -
-
- DirectiveWeWantToMock 2: render {{ z.a }} -
- -
MyPipe: {{ 'text' | MyPipe }}
-
PipeWeDontWantToMock: {{ 'text' | PipeWeDontWantToMock }}
-
PipeWeWantToMock: {{ 'text' | PipeWeWantToMock }}
-
PipeWeWantToCustomize: {{ 'text' | PipeWeWantToCustomize }}
-
PipeWeWantToRestore: {{ 'text' | PipeWeWantToRestore }}
- -
INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK: {{ t1v }}
-
INJECTION_TOKEN_WE_WANT_TO_MOCK: {{ t2v }}
-
INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE: {{ t3v }}
- -
anythingWeWant1: {{ anythingWeWant1?.getName() }}
-
anythingWeWant2: {{ anythingWeWant2?.getName() }}
-
myCustomProvider1: {{ myCustomProvider1?.getName() }}
-
myCustomProvider2: {{ myCustomProvider2?.getName() }}
-
myCustomProvider3: {{ myCustomProvider3?.getName() }}
- -
myService1: {{ myService1?.getName() }}
-
myService2: {{ myService2?.getName() }}
-
serviceWeDontWantToMock: {{ serviceWeDontWantToMock?.getName() }}
-
serviceWeWantToCustomize: {{ serviceWeWantToCustomize?.getName() }}
-
serviceWeWantToMock: {{ serviceWeWantToMock?.getName() }}
- - - -
ComponentStructural: {{ value }} {{ b.z }}
-
-
- `, -}) -export class MyComponent { - public readonly anythingWeWant1: AnythingWeWant1; - public readonly anythingWeWant2: AnythingWeWant2; - public readonly myCustomProvider1: MyCustomProvider1; - public readonly myCustomProvider2: MyCustomProvider2; - public readonly myCustomProvider3: MyCustomProvider3; - public readonly myService1: MyService1; - public readonly myService2: MyService2; - public readonly serviceWeDontWantToMock: ServiceWeDontWantToMock; - public readonly serviceWeWantToCustomize: ServiceWeWantToCustomize; - public readonly serviceWeWantToMock: ServiceWeWantToMock; - public readonly t1v: string; - public readonly t2v: string; - public readonly t3v: string; - - constructor( - @Optional() @Inject(INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK) t1: string, - @Optional() @Inject(INJECTION_TOKEN_WE_WANT_TO_MOCK) t2: string, - @Optional() @Inject(INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE) t3: string, - @Optional() anythingWeWant1: AnythingWeWant1, - @Optional() anythingWeWant2: AnythingWeWant2, - @Optional() myCustomProvider1: MyCustomProvider1, - @Optional() myCustomProvider2: MyCustomProvider2, - @Optional() myCustomProvider3: MyCustomProvider3, - @Optional() myService1: MyService1, - @Optional() myService2: MyService2, - @Optional() serviceWeDontWantToMock: ServiceWeDontWantToMock, - @Optional() serviceWeWantToMock: ServiceWeWantToMock, - @Optional() serviceWeWantToCustomize: ServiceWeWantToCustomize - ) { - this.t1v = t1; - this.t2v = t2; - this.t3v = t3; - this.anythingWeWant1 = anythingWeWant1; - this.anythingWeWant2 = anythingWeWant2; - this.myCustomProvider1 = myCustomProvider1; - this.myCustomProvider2 = myCustomProvider2; - this.myCustomProvider3 = myCustomProvider3; - this.myService1 = myService1; - this.myService2 = myService2; - this.serviceWeDontWantToMock = serviceWeDontWantToMock; - this.serviceWeWantToCustomize = serviceWeWantToCustomize; - this.serviceWeWantToMock = serviceWeWantToMock; - } -} - -@Component({ - selector: 'component-1', - template: 'MyComponent1', -}) -export class MyComponent1 {} - -@Component({ - selector: 'component-2', - template: 'MyComponent2', -}) -export class MyComponent2 {} - -@Component({ - selector: 'component-3', - template: 'MyComponent3', -}) -export class MyComponent3 {} - -@Component({ - selector: 'dont-want', - template: 'ComponentWeDontWantToMock', -}) -export class ComponentWeDontWantToMock {} - -@Component({ - selector: 'do-want', - template: 'ComponentWeWantToMock', -}) -export class ComponentWeWantToMock {} diff --git a/examples-jasmine/MockComponent/dependency.component.ts b/examples-jasmine/MockComponent/dependency.component.ts deleted file mode 100644 index 7a9a43daf1..0000000000 --- a/examples-jasmine/MockComponent/dependency.component.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Component, ContentChild, EventEmitter, Input, Output, TemplateRef } from '@angular/core'; - -import { staticFalse } from '../../tests-jasmine'; - -@Component({ - selector: 'dependency-component-selector', - template: `dependency`, -}) -export class DependencyComponent { - @ContentChild('something', { ...staticFalse }) - injectedSomething: TemplateRef<{}>; - - @Input() - someInput = ''; - - @Output() - someOutput = new EventEmitter(); -} diff --git a/examples-jasmine/MockModule/dependency.component.ts b/examples-jasmine/MockModule/dependency.component.ts deleted file mode 100644 index 7a9a43daf1..0000000000 --- a/examples-jasmine/MockModule/dependency.component.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Component, ContentChild, EventEmitter, Input, Output, TemplateRef } from '@angular/core'; - -import { staticFalse } from '../../tests-jasmine'; - -@Component({ - selector: 'dependency-component-selector', - template: `dependency`, -}) -export class DependencyComponent { - @ContentChild('something', { ...staticFalse }) - injectedSomething: TemplateRef<{}>; - - @Input() - someInput = ''; - - @Output() - someOutput = new EventEmitter(); -} diff --git a/examples-jasmine/MockRender/MockRender.spec.ts b/examples-jasmine/MockRender/MockRender.spec.ts deleted file mode 100644 index 2de28ccf59..0000000000 --- a/examples-jasmine/MockRender/MockRender.spec.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { TestBed } from '@angular/core/testing'; -import { MockModule, MockRender, ngMocks } from 'ng-mocks'; - -import { DependencyModule } from './dependency.module'; -import { TestedComponent } from './tested.component'; - -describe('MockRender', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [TestedComponent], - imports: [MockModule(DependencyModule)], - }); - }); - - it('renders template', () => { - const spy = jasmine.createSpy(); - const fixture = MockRender( - ` - - - something as ng-template - - something as ng-content - - `, - { - myListener1: spy, - myParam1: 'something1', - } - ); - - // ngMocks.input helps to get current value of an input on a related debugElement. - expect(ngMocks.input(fixture.point, 'value1')).toEqual('something1'); - expect(ngMocks.input(fixture.point, 'value2')).toEqual('check'); - - // ngMocks.output does the same with outputs. - ngMocks.output(fixture.point, 'trigger').emit('foo1'); - expect(spy).toHaveBeenCalledWith('foo1'); - }); - - it('renders component', () => { - const spy = jasmine.createSpy(); - // generates template like: - // - // and returns fixture with a component with properties value1, value2 and empty callback trigger. - const fixture = MockRender(TestedComponent, { - trigger: spy, - value1: 'something2', - }); - - // ngMocks.input helps to get current value of an input on a related debugElement. - expect(ngMocks.input(fixture.point, 'value1')).toEqual('something2'); - expect(ngMocks.input(fixture.point, 'value2')).toBeUndefined(); - - // ngMocks.output does the same with outputs. - ngMocks.output(fixture.point, 'trigger').emit('foo2'); - expect(spy).toHaveBeenCalledWith('foo2'); - - // checking that an updated value has been passed into testing component. - fixture.componentInstance.value1 = 'updated'; - fixture.detectChanges(); - expect(ngMocks.input(fixture.point, 'value1')).toEqual('updated'); - }); -}); diff --git a/examples-jasmine/MockRender/dependency.component.ts b/examples-jasmine/MockRender/dependency.component.ts deleted file mode 100644 index 7a9a43daf1..0000000000 --- a/examples-jasmine/MockRender/dependency.component.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Component, ContentChild, EventEmitter, Input, Output, TemplateRef } from '@angular/core'; - -import { staticFalse } from '../../tests-jasmine'; - -@Component({ - selector: 'dependency-component-selector', - template: `dependency`, -}) -export class DependencyComponent { - @ContentChild('something', { ...staticFalse }) - injectedSomething: TemplateRef<{}>; - - @Input() - someInput = ''; - - @Output() - someOutput = new EventEmitter(); -} diff --git a/examples-jasmine/NG_MOCKS/fixtures.components.ts b/examples-jasmine/NG_MOCKS/fixtures.components.ts deleted file mode 100644 index f0c247c9f3..0000000000 --- a/examples-jasmine/NG_MOCKS/fixtures.components.ts +++ /dev/null @@ -1,110 +0,0 @@ -import { Component, ContentChild, Inject, Input, Optional, TemplateRef } from '@angular/core'; - -import { staticFalse } from '../../tests-jasmine'; - -import { - AnythingWeWant1, - AnythingWeWant2, - MyCustomProvider1, - MyCustomProvider2, - MyCustomProvider3, - MyService1, - MyService2, - ServiceWeDontWantToMock, - ServiceWeWantToCustomize, - ServiceWeWantToMock, -} from './fixtures.services'; -import { - INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK, - INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE, - INJECTION_TOKEN_WE_WANT_TO_MOCK, -} from './fixtures.tokens'; - -@Component({ - selector: 'component-structural', - template: '', -}) -export class ComponentContentChild { - @ContentChild('block', { ...staticFalse }) injectedBlock: TemplateRef; - @Input() items?: T[]; -} - -@Component({ - selector: 'my-component', - template: '', -}) -export class MyComponent { - public readonly anythingWeWant1: AnythingWeWant1; - public readonly anythingWeWant2: AnythingWeWant2; - public readonly myCustomProvider1: MyCustomProvider1; - public readonly myCustomProvider2: MyCustomProvider2; - public readonly myCustomProvider3: MyCustomProvider3; - public readonly myService1: MyService1; - public readonly myService2: MyService2; - public readonly serviceWeDontWantToMock: ServiceWeDontWantToMock; - public readonly serviceWeWantToCustomize: ServiceWeWantToCustomize; - public readonly serviceWeWantToMock: ServiceWeWantToMock; - public readonly t1v: string; - public readonly t2v: string; - public readonly t3v: string; - - constructor( - @Optional() @Inject(INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK) t1: string, - @Optional() @Inject(INJECTION_TOKEN_WE_WANT_TO_MOCK) t2: string, - @Optional() @Inject(INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE) t3: string, - @Optional() anythingWeWant1: AnythingWeWant1, - @Optional() anythingWeWant2: AnythingWeWant2, - @Optional() myCustomProvider1: MyCustomProvider1, - @Optional() myCustomProvider2: MyCustomProvider2, - @Optional() myCustomProvider3: MyCustomProvider3, - @Optional() myService1: MyService1, - @Optional() myService2: MyService2, - @Optional() serviceWeDontWantToMock: ServiceWeDontWantToMock, - @Optional() serviceWeWantToMock: ServiceWeWantToMock, - @Optional() serviceWeWantToCustomize: ServiceWeWantToCustomize - ) { - this.t1v = t1; - this.t2v = t2; - this.t3v = t3; - this.anythingWeWant1 = anythingWeWant1; - this.anythingWeWant2 = anythingWeWant2; - this.myCustomProvider1 = myCustomProvider1; - this.myCustomProvider2 = myCustomProvider2; - this.myCustomProvider3 = myCustomProvider3; - this.myService1 = myService1; - this.myService2 = myService2; - this.serviceWeDontWantToMock = serviceWeDontWantToMock; - this.serviceWeWantToCustomize = serviceWeWantToCustomize; - this.serviceWeWantToMock = serviceWeWantToMock; - } -} - -@Component({ - selector: 'component-1', - template: 'MyComponent1', -}) -export class MyComponent1 {} - -@Component({ - selector: 'component-2', - template: 'MyComponent2', -}) -export class MyComponent2 {} - -@Component({ - selector: 'component-3', - template: 'MyComponent3', -}) -export class MyComponent3 {} - -@Component({ - selector: 'dont-want', - template: 'ComponentWeDontWantToMock', -}) -export class ComponentWeDontWantToMock {} - -@Component({ - selector: 'do-want', - template: 'ComponentWeWantToMock', -}) -export class ComponentWeWantToMock {} diff --git a/examples-jest/MockBuilder/MockBuilder.spec.ts b/examples-jest/MockBuilder/MockBuilder.spec.ts deleted file mode 100644 index 6ddcc8ae47..0000000000 --- a/examples-jest/MockBuilder/MockBuilder.spec.ts +++ /dev/null @@ -1,223 +0,0 @@ -import { HttpBackend, HttpClientModule } from '@angular/common/http'; -import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { inject, TestBed } from '@angular/core/testing'; -import { MockBuilder, MockRender } from 'ng-mocks'; - -import { - ComponentContentChild, - ComponentWeDontWantToMock, - ComponentWeWantToMock, - MyComponent, - MyComponent1, - MyComponent2, - MyComponent3, -} from './fixtures.components'; -import { DirectiveWeDontWantToMock, DirectiveWeWantToMock, MyDirective } from './fixtures.directives'; -import { ModuleWeDontWantToMock, ModuleWeWantToMockBesidesMyModule, MyModule } from './fixtures.modules'; -import { - MyPipe, - PipeWeDontWantToMock, - PipeWeWantToCustomize, - PipeWeWantToMock, - PipeWeWantToRestore, -} from './fixtures.pipes'; -import { - AnythingWeWant1, - AnythingWeWant2, - MyCustomProvider1, - MyCustomProvider2, - MyCustomProvider3, - MyService1, - MyService2, - ServiceWeDontWantToMock, - ServiceWeWantToCustomize, - ServiceWeWantToMock, - TheSameAsAnyProvider, -} from './fixtures.services'; -import { - INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK, - INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE, - INJECTION_TOKEN_WE_WANT_TO_MOCK, -} from './fixtures.tokens'; - -describe('MockBuilder:simple', () => { - beforeEach(async () => { - const ngModule = MockBuilder(MyComponent, MyModule) - // mocking configuration here - .build(); - - // now ngModule is - // { - // imports: [MockModule(MyModule)], // but MyComponent wasn't mocked for the testing purposes. - // } - // and we can simply pass it to the TestBed. - return TestBed.configureTestingModule(ngModule).compileComponents(); - }); - - it('should render content ignoring all dependencies', () => { - const fixture = MockRender(MyComponent); - expect(fixture).toBeDefined(); - expect(fixture.debugElement.nativeElement.innerHTML).toContain('
My Content
'); - }); -}); - -describe('MockBuilder:deep', () => { - beforeEach(async () => { - const ngModule = MockBuilder(MyComponent, MyModule) - .mock(ComponentContentChild, { - render: { - block: { - $implicit: '-$implicit-', - variables: { a: { z: 'b' } }, - }, - }, - }) - - .keep(ModuleWeDontWantToMock, { - dependency: true, - }) - .keep(ComponentWeDontWantToMock, { - dependency: true, - }) - .keep(DirectiveWeDontWantToMock, { - dependency: true, - }) - .keep(PipeWeDontWantToMock, { - dependency: true, - }) - .keep(ServiceWeDontWantToMock) - .keep(INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK) - - // The same can be done with Components, Directives and Pipes. - // For Providers use .provider() or .mock(). - .replace(HttpClientModule, HttpClientTestingModule, { - dependency: true, - }) - - .mock(ModuleWeWantToMockBesidesMyModule, { - dependency: true, - }) - .mock(ComponentWeWantToMock, { - dependency: true, - }) - .mock(DirectiveWeWantToMock, { - dependency: true, - render: { - $implicit: { a: '$' }, - variables: { a: { b: 'b' } }, - }, - }) - .mock(PipeWeWantToMock, { - dependency: true, - }) - .mock(ServiceWeWantToMock) // makes all methods an empty function - .mock(INJECTION_TOKEN_WE_WANT_TO_MOCK) // makes its value undefined - - .mock(PipeWeWantToCustomize, value => 'My Custom Result') - .mock(PipeWeWantToRestore, value => 'My Restored Pipe') - .mock(ServiceWeWantToCustomize, { prop1: true, getName: () => 'My Customized String' }) - .mock(INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE, 'My_Token') - - // All providers will be set into the TestModule. - .provide({ - provide: AnythingWeWant1, - useValue: new TheSameAsAnyProvider(), - }) - .provide({ - provide: AnythingWeWant2, - useFactory: () => new TheSameAsAnyProvider(), - }) - .provide(MyCustomProvider1) - .provide([MyCustomProvider2, MyCustomProvider3]) - - // Now the pipe won't be mocked. - .keep(PipeWeWantToRestore) - - // Extra configuration. - .keep(MyDirective) - .keep(MyPipe) - .mock(MyService1) - .keep(MyService2) - - // Even it belongs to the module that is marked as kept, the component will be mocked and replaced. - .mock(MyComponent3) - - // and now we want to build our NgModule. - .build(); - TestBed.configureTestingModule(ngModule); - - // Extra configuration - TestBed.overrideTemplate(MyComponent1, 'If we need to tune testBed'); - TestBed.overrideTemplate(MyComponent2, 'More callbacks'); - - return TestBed.compileComponents(); - }); - - it('should render', inject([HttpBackend], (httpBackend: HttpBackend) => { - const fixture = MockRender(MyComponent); - expect(fixture).toBeDefined(); - const content = fixture.debugElement.nativeElement.innerHTML.replace(//gm, ''); - expect(content).toContain('
My Content
'); - - expect(content).toContain('
MyComponent1: If we need to tune testBed
'); - expect(content).toContain('
MyComponent2: More callbacks
'); - expect(content).toContain('
MyComponent3:
'); - expect(content).toContain('
ComponentWeDontWantToMock: ComponentWeDontWantToMock
'); - expect(content).toContain('
ComponentWeWantToMock:
'); - expect(content).toContain('
ComponentStructural: -$implicit- b
'); - - expect(content).toContain('
MyDirective:
'); - expect(content).toContain('
DirectiveWeDontWantToMock:
'); - expect(content).toContain('DirectiveWeWantToMock 1: render b'); - expect(content).toContain('DirectiveWeWantToMock 2: render $'); - - expect(content).toContain('
MyPipe: MyPipe:text
'); - expect(content).toContain('
PipeWeDontWantToMock: PipeWeDontWantToMock:text
'); - expect(content).toContain('
PipeWeWantToMock:
'); - expect(content).toContain('
PipeWeWantToCustomize: My Custom Result
'); - expect(content).toContain('
PipeWeWantToRestore: PipeWeWantToRestore:text
'); - - expect(content).toContain('
INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK: INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK
'); - expect(content).toContain('
INJECTION_TOKEN_WE_WANT_TO_MOCK:
'); - expect(content).toContain('
INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE: My_Token
'); - - expect(content).toContain('
anythingWeWant1: TheSameAsAnyProvider
'); - expect(content).toContain('
anythingWeWant2: TheSameAsAnyProvider
'); - expect(content).toContain('
myCustomProvider1: MyCustomProvider1
'); - expect(content).toContain('
myCustomProvider2: MyCustomProvider2
'); - expect(content).toContain('
myCustomProvider3: MyCustomProvider3
'); - - expect(content).toContain('
myService1:
'); - expect(content).toContain('
myService2: MyService2
'); - expect(content).toContain('
serviceWeDontWantToMock: ServiceWeDontWantToMock
'); - expect(content).toContain('
serviceWeWantToCustomize: My Customized String
'); - expect(content).toContain('
serviceWeWantToMock:
'); - - // Checking that replacement works. - expect(httpBackend.constructor).toBeDefined(); - expect(httpBackend.constructor.name).toEqual('HttpClientTestingBackend'); - })); -}); - -describe('MockBuilder:promise', () => { - beforeEach(() => - MockBuilder() - .keep(MyComponent1) - .keep(MyComponent2) - - // In case if you need extra customization of TestBed in promise way. - .beforeCompileComponents(testBed => { - testBed.overrideTemplate(MyComponent1, 'If we need to tune testBed'); - }) - .beforeCompileComponents(testBed => { - testBed.overrideTemplate(MyComponent2, 'More callbacks'); - }) - ); - - it('should render content ignoring all dependencies', () => { - const fixture = MockRender(''); - expect(fixture).toBeDefined(); - expect(fixture.debugElement.nativeElement.innerHTML).toContain('If we need to tune testBed'); - expect(fixture.debugElement.nativeElement.innerHTML).toContain('More callbacks'); - }); -}); diff --git a/examples-jest/MockBuilder/fixtures.directives.ts b/examples-jest/MockBuilder/fixtures.directives.ts deleted file mode 100644 index 6dbb2e86f8..0000000000 --- a/examples-jest/MockBuilder/fixtures.directives.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Directive } from '@angular/core'; - -@Directive({ - selector: 'MyDirective', -}) -export class MyDirective {} - -@Directive({ - selector: 'WeDontWantToMock', -}) -export class DirectiveWeDontWantToMock {} - -@Directive({ - selector: '[WeWantToMock]', -}) -export class DirectiveWeWantToMock {} diff --git a/examples-jest/MockBuilder/fixtures.modules.ts b/examples-jest/MockBuilder/fixtures.modules.ts deleted file mode 100644 index 264824c22b..0000000000 --- a/examples-jest/MockBuilder/fixtures.modules.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { HttpClientModule } from '@angular/common/http'; -import { NgModule } from '@angular/core'; - -import { - ComponentContentChild, - ComponentWeDontWantToMock, - ComponentWeWantToMock, - MyComponent, - MyComponent1, - MyComponent2, - MyComponent3, -} from './fixtures.components'; -import { DirectiveWeDontWantToMock, DirectiveWeWantToMock, MyDirective } from './fixtures.directives'; -import { - MyPipe, - PipeWeDontWantToMock, - PipeWeWantToCustomize, - PipeWeWantToMock, - PipeWeWantToRestore, -} from './fixtures.pipes'; -import { - MyService1, - MyService2, - ServiceWeDontWantToMock, - ServiceWeWantToCustomize, - ServiceWeWantToMock, -} from './fixtures.services'; -import { - INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK, - INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE, - INJECTION_TOKEN_WE_WANT_TO_MOCK, -} from './fixtures.tokens'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - declarations: [ - ComponentWeDontWantToMock, - ComponentWeWantToMock, - DirectiveWeDontWantToMock, - DirectiveWeWantToMock, - PipeWeDontWantToMock, - PipeWeWantToMock, - PipeWeWantToCustomize, - PipeWeWantToRestore, - ], - exports: [ - ComponentWeDontWantToMock, - ComponentWeWantToMock, - DirectiveWeDontWantToMock, - DirectiveWeWantToMock, - PipeWeDontWantToMock, - PipeWeWantToMock, - PipeWeWantToCustomize, - PipeWeWantToRestore, - ], - providers: [ - ServiceWeDontWantToMock, - ServiceWeWantToMock, - { - provide: INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK, - useValue: 'INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK', - }, - { - provide: INJECTION_TOKEN_WE_WANT_TO_MOCK, - useValue: 'INJECTION_TOKEN_WE_WANT_TO_MOCK', - }, - { - provide: INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE, - useValue: 'INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE', - }, - ], -}) -export class ModuleWeWantToMockBesidesMyModule {} - -@NgModule({ - declarations: [MyComponent1, MyComponent2, MyComponent3, ComponentContentChild], - exports: [MyComponent1, MyComponent2, MyComponent3, ComponentContentChild], - imports: [CommonModule], -}) -export class ModuleWeDontWantToMock {} - -@NgModule({ - declarations: [MyComponent, MyDirective, MyPipe], - exports: [MyComponent, MyDirective, MyPipe], - imports: [HttpClientModule, ModuleWeWantToMockBesidesMyModule, ModuleWeDontWantToMock], - providers: [MyService1, MyService2, ServiceWeWantToCustomize], -}) -export class MyModule {} diff --git a/examples-jest/MockBuilder/fixtures.pipes.ts b/examples-jest/MockBuilder/fixtures.pipes.ts deleted file mode 100644 index a1034148cb..0000000000 --- a/examples-jest/MockBuilder/fixtures.pipes.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { Pipe, PipeTransform } from '@angular/core'; - -@Pipe({ - name: 'MyPipe', -}) -export class MyPipe implements PipeTransform { - protected prefix = 'MyPipe:'; - - public transform(value: any, ...args: any[]): any { - return this.prefix + value; - } -} - -@Pipe({ - name: 'PipeWeDontWantToMock', -}) -export class PipeWeDontWantToMock implements PipeTransform { - protected prefix = 'PipeWeDontWantToMock:'; - - public transform(value: any, ...args: any[]): any { - return this.prefix + value; - } -} - -@Pipe({ - name: 'PipeWeWantToMock', -}) -export class PipeWeWantToMock implements PipeTransform { - protected prefix = 'PipeWeWantToMock:'; - - public transform(value: any, ...args: any[]): any { - return this.prefix + value; - } -} - -@Pipe({ - name: 'PipeWeWantToCustomize', -}) -export class PipeWeWantToCustomize implements PipeTransform { - protected prefix = 'PipeWeWantToCustomize:'; - - public transform(value: any, ...args: any[]): any { - return this.prefix + value; - } -} - -@Pipe({ - name: 'PipeWeWantToRestore', -}) -export class PipeWeWantToRestore implements PipeTransform { - protected prefix = 'PipeWeWantToRestore:'; - - public transform(value: any, ...args: any[]): any { - return this.prefix + value; - } -} diff --git a/examples-jest/MockBuilder/fixtures.services.ts b/examples-jest/MockBuilder/fixtures.services.ts deleted file mode 100644 index 4fb36c910a..0000000000 --- a/examples-jest/MockBuilder/fixtures.services.ts +++ /dev/null @@ -1,100 +0,0 @@ -import { Injectable } from '@angular/core'; - -@Injectable() -export class MyService1 { - protected value = 'MyService1'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class MyService2 { - protected value = 'MyService2'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class ServiceWeDontWantToMock { - protected value = 'ServiceWeDontWantToMock'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class ServiceWeWantToMock { - protected value = 'ServiceWeWantToMock'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class ServiceWeWantToCustomize { - protected value = 'ServiceWeWantToCustomize'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class AnythingWeWant1 { - protected value = 'AnythingWeWant1'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class TheSameAsAnyProvider { - protected value = 'TheSameAsAnyProvider'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class AnythingWeWant2 { - protected value = 'AnythingWeWant2'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class MyCustomProvider1 { - protected value = 'MyCustomProvider1'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class MyCustomProvider2 { - protected value = 'MyCustomProvider2'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class MyCustomProvider3 { - protected value = 'MyCustomProvider3'; - - public getName() { - return this.value; - } -} diff --git a/examples-jest/MockBuilder/fixtures.tokens.ts b/examples-jest/MockBuilder/fixtures.tokens.ts deleted file mode 100644 index 8d527302f7..0000000000 --- a/examples-jest/MockBuilder/fixtures.tokens.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { InjectionToken } from '@angular/core'; - -export const INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK = new InjectionToken('INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK'); - -export const INJECTION_TOKEN_WE_WANT_TO_MOCK = new InjectionToken('INJECTION_TOKEN_WE_WANT_TO_MOCK'); - -export const INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE = new InjectionToken('INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE'); diff --git a/examples-jest/MockComponent/MockComponent.spec.ts b/examples-jest/MockComponent/MockComponent.spec.ts deleted file mode 100644 index fea2e01e66..0000000000 --- a/examples-jest/MockComponent/MockComponent.spec.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { MockComponent, MockedComponent, MockRender, ngMocks } from 'ng-mocks'; - -import { DependencyComponent } from './dependency.component'; -import { TestedComponent } from './tested.component'; - -describe('MockComponent', () => { - let fixture: ComponentFixture; - let component: TestedComponent; - - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [TestedComponent, MockComponent(DependencyComponent)], - }); - - fixture = TestBed.createComponent(TestedComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should send the correct value to the dependency component input', () => { - // the same as fixture.debugElement.query(By.css('dependency-component-selector')).componentInstance - // but properly typed. - const mockedComponent = ngMocks.find(fixture.debugElement, 'dependency-component-selector') - .componentInstance; - - // let's pretend Dependency Component (unmocked) has 'someInput' as an input - // the input value will be passed into the mocked component so you can assert on it - component.value = 'foo'; - fixture.detectChanges(); - - // if you casted mockedComponent as the original component type then this is type safe - expect(mockedComponent.someInput).toEqual('foo'); - }); - - it('should do something when the dependency component emits on its output', () => { - spyOn(component, 'trigger'); - const mockedComponent = ngMocks.find(fixture.debugElement, DependencyComponent).componentInstance; - - // again, let's pretend DependencyComponent has an output called 'someOutput' - // emit on the output that MockComponent setup when generating the mock of Dependency Component - // if you casted mockedComponent as the original component type then this is type safe - mockedComponent.someOutput.emit({ - payload: 'foo', - }); - - // assert on some side effect - expect(component.trigger).toHaveBeenCalledWith({ - payload: 'foo', - }); - }); - - it('should render something inside of the dependency component', () => { - const localFixture = MockRender(` - -

inside content

-
- `); - - // because component does not have any @ContentChild we can access html directly. - // assert on some side effect - const mockedNgContent = localFixture.point.nativeElement.innerHTML; - expect(mockedNgContent).toContain('

inside content

'); - }); - - it('should render something inside of the dependency component', () => { - const localFixture = MockRender>(` - -

inside template

-

inside content

-
- `); - - // injected ng-content says as it was. - const mockedNgContent = localFixture.point.nativeElement.innerHTML; - expect(mockedNgContent).toContain('

inside content

'); - - // because component does have @ContentChild we need to render them first with proper context. - const mockedComponent = localFixture.point.componentInstance; - mockedComponent.__render('something'); - localFixture.detectChanges(); - - const mockedNgTemplate = ngMocks.find(localFixture.debugElement, '[data-key="something"]').nativeElement.innerHTML; - expect(mockedNgTemplate).toContain('

inside template

'); - }); -}); diff --git a/examples-jest/MockComponent/tested.component.ts b/examples-jest/MockComponent/tested.component.ts deleted file mode 100644 index 978a4d2596..0000000000 --- a/examples-jest/MockComponent/tested.component.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'tested', - template: ` - - `, -}) -export class TestedComponent { - value = ''; - trigger = (obj: any) => {}; -} diff --git a/examples-jest/MockDirective-Attribute/MockDirective.spec.ts b/examples-jest/MockDirective-Attribute/MockDirective.spec.ts deleted file mode 100644 index 881fa72b60..0000000000 --- a/examples-jest/MockDirective-Attribute/MockDirective.spec.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { MockDirective, ngMocks } from 'ng-mocks'; - -import { DependencyDirective } from './dependency.directive'; -import { TestedComponent } from './tested.component'; - -describe('MockDirective', () => { - let fixture: ComponentFixture; - let component: TestedComponent; - - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [TestedComponent, MockDirective(DependencyDirective)], - }); - - fixture = TestBed.createComponent(TestedComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should send the correct value to the dependency component input', () => { - component.value = 'foo'; - fixture.detectChanges(); - - // let's pretend Dependency Directive (unmocked) has 'someInput' as an input - // the input value will be passed into the mocked directive so you can assert on it - const mockedDirectiveInstance = ngMocks.get(ngMocks.find(fixture.debugElement, 'span'), DependencyDirective); - - expect(mockedDirectiveInstance.someInput).toEqual('foo'); - // assert on some side effect - }); - - it('should do something when the dependency directive emits on its output', () => { - spyOn(component, 'trigger'); - fixture.detectChanges(); - - // again, let's pretend DependencyDirective has an output called 'someOutput' - // emit on the output that MockDirective setup when generating the mock of Dependency Directive - const mockedDirectiveInstance = ngMocks.get(ngMocks.find(fixture.debugElement, 'span'), DependencyDirective); - mockedDirectiveInstance.someOutput.emit({ - payload: 'foo', - }); // if you casted mockedDirective as the original component type then this is type safe - // assert on some side effect - }); -}); diff --git a/examples-jest/MockDirective-Attribute/dependency.directive.ts b/examples-jest/MockDirective-Attribute/dependency.directive.ts deleted file mode 100644 index 75730cb0ea..0000000000 --- a/examples-jest/MockDirective-Attribute/dependency.directive.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Directive, EventEmitter, Input, Output } from '@angular/core'; - -@Directive({ - selector: '[dependency]', -}) -export class DependencyDirective { - @Input('dependency-input') - someInput = ''; - - @Output('dependency-output') - someOutput = new EventEmitter(); -} diff --git a/examples-jest/MockDirective-Attribute/tested.component.ts b/examples-jest/MockDirective-Attribute/tested.component.ts deleted file mode 100644 index a77d300f58..0000000000 --- a/examples-jest/MockDirective-Attribute/tested.component.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'tested', - template: ` `, -}) -export class TestedComponent { - value = ''; - trigger = () => {}; -} diff --git a/examples-jest/MockDirective-Structural/MockDirective.spec.ts b/examples-jest/MockDirective-Structural/MockDirective.spec.ts deleted file mode 100644 index c3dcaa886a..0000000000 --- a/examples-jest/MockDirective-Structural/MockDirective.spec.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { MockDirective, MockedDirective, ngMocks } from 'ng-mocks'; - -import { DependencyDirective } from './dependency.directive'; -import { TestedComponent } from './tested.component'; - -describe('MockDirective', () => { - let fixture: ComponentFixture; - let component: TestedComponent; - - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [TestedComponent, MockDirective(DependencyDirective)], - }); - - fixture = TestBed.createComponent(TestedComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should send the correct value to the dependency component input', () => { - component.value = 'foo'; - fixture.detectChanges(); - - // IMPORTANT: by default structural directives aren't rendered. - // Because we can't automatically detect when and with which context they should be rendered. - // Usually developer knows context and can render it manually with proper setup. - const mockedDirectiveInstance = ngMocks.findInstance(fixture.debugElement, DependencyDirective) as MockedDirective< - DependencyDirective - >; - - // now we assert that nothing has been rendered inside of the structural directive by default. - expect(fixture.debugElement.nativeElement.innerHTML).not.toContain('>content<'); - - // and now we render it manually. - mockedDirectiveInstance.__render(); - expect(fixture.debugElement.nativeElement.innerHTML).toContain('>content<'); - - // let's pretend Dependency Directive (unmocked) has 'someInput' as an input - // the input value will be passed into the mocked directive so you can assert on it - expect(mockedDirectiveInstance.someInput).toEqual('foo'); - // assert on some side effect - }); -}); diff --git a/examples-jest/MockDirective-Structural/dependency.directive.ts b/examples-jest/MockDirective-Structural/dependency.directive.ts deleted file mode 100644 index 9726d536fa..0000000000 --- a/examples-jest/MockDirective-Structural/dependency.directive.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Directive, EventEmitter, Input, Output } from '@angular/core'; - -@Directive({ - selector: '[dependency]', -}) -export class DependencyDirective { - @Input('dependency') - someInput = ''; - - @Output('dependency-output') - someOutput = new EventEmitter(); -} diff --git a/examples-jest/MockDirective-Structural/tested.component.ts b/examples-jest/MockDirective-Structural/tested.component.ts deleted file mode 100644 index ebca728b84..0000000000 --- a/examples-jest/MockDirective-Structural/tested.component.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'tested', - template: ` content `, -}) -export class TestedComponent { - value = ''; - trigger = () => {}; -} diff --git a/examples-jest/MockModule/MockModule.spec.ts b/examples-jest/MockModule/MockModule.spec.ts deleted file mode 100644 index d514501448..0000000000 --- a/examples-jest/MockModule/MockModule.spec.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { MockModule } from 'ng-mocks'; - -import { DependencyModule } from './dependency.module'; -import { TestedComponent } from './tested.component'; - -describe('MockModule', () => { - let fixture: ComponentFixture; - let component: TestedComponent; - - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [TestedComponent], - imports: [MockModule(DependencyModule)], - }); - - fixture = TestBed.createComponent(TestedComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('renders nothing without any error', () => { - expect(component).toBeTruthy(); - }); -}); diff --git a/examples-jest/MockModule/dependency.module.ts b/examples-jest/MockModule/dependency.module.ts deleted file mode 100644 index 8e602c0496..0000000000 --- a/examples-jest/MockModule/dependency.module.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { CommonModule } from '@angular/common'; -import { NgModule } from '@angular/core'; -import { DependencyComponent } from './dependency.component'; - -@NgModule({ - declarations: [DependencyComponent], - entryComponents: [DependencyComponent], - imports: [CommonModule], -}) -export class DependencyModule {} diff --git a/examples-jest/MockModule/tested.component.ts b/examples-jest/MockModule/tested.component.ts deleted file mode 100644 index 8011f9cca5..0000000000 --- a/examples-jest/MockModule/tested.component.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'tested', - template: ` - - `, -}) -export class TestedComponent { - value = ''; - trigger = () => {}; -} diff --git a/examples-jest/MockPipe/MockPipe.spec.ts b/examples-jest/MockPipe/MockPipe.spec.ts deleted file mode 100644 index 1af7db41d7..0000000000 --- a/examples-jest/MockPipe/MockPipe.spec.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { MockPipe, ngMocks } from 'ng-mocks'; - -import { DependencyPipe } from './dependency.pipe'; -import { TestedComponent } from './tested.component'; - -describe('MockPipe', () => { - let fixture: ComponentFixture; - - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [ - TestedComponent, - - // alternatively you can use MockPipes to mock multiple but you lose the ability to override - MockPipe(DependencyPipe, (...args: string[]) => JSON.stringify(args)), - ], - }); - - fixture = TestBed.createComponent(TestedComponent); - fixture.detectChanges(); - }); - - describe('with transform override', () => { - it('should return the result of the provided transform function', () => { - const pipeElement = ngMocks.find(fixture.debugElement, 'span'); - expect(pipeElement.nativeElement.innerHTML).toEqual('["foo"]'); - }); - }); -}); diff --git a/examples-jest/MockPipe/dependency.pipe.ts b/examples-jest/MockPipe/dependency.pipe.ts deleted file mode 100644 index 103b1fe91b..0000000000 --- a/examples-jest/MockPipe/dependency.pipe.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Pipe, PipeTransform } from '@angular/core'; - -@Pipe({ name: 'dependency' }) -export class DependencyPipe implements PipeTransform { - transform = (name: string): string => `hi ${name}`; -} diff --git a/examples-jest/MockPipe/tested.component.ts b/examples-jest/MockPipe/tested.component.ts deleted file mode 100644 index bfa1298908..0000000000 --- a/examples-jest/MockPipe/tested.component.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'tested', - template: ` {{ 'foo' | dependency }} `, -}) -export class TestedComponent {} diff --git a/examples-jest/MockReactiveForms/MockReactiveForms.spec.ts b/examples-jest/MockReactiveForms/MockReactiveForms.spec.ts deleted file mode 100644 index 93f1440b57..0000000000 --- a/examples-jest/MockReactiveForms/MockReactiveForms.spec.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { ReactiveFormsModule } from '@angular/forms'; -import { MockComponent, MockedComponent, ngMocks } from 'ng-mocks'; - -import { DependencyComponent } from './dependency.component'; -import { TestedComponent } from './tested.component'; - -describe('MockReactiveForms', () => { - let fixture: ComponentFixture; - let component: TestedComponent; - - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [TestedComponent, MockComponent(DependencyComponent)], - imports: [ReactiveFormsModule], - }); - - fixture = TestBed.createComponent(TestedComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should send the correct value to the dependency component input', () => { - const mockedReactiveFormComponent = ngMocks.find>( - fixture.debugElement, - 'dependency-component-selector' - ).componentInstance; - - mockedReactiveFormComponent.__simulateChange('foo'); - expect(component.formControl.value).toBe('foo'); - - spyOn(mockedReactiveFormComponent, 'writeValue'); - component.formControl.setValue('bar'); - expect(mockedReactiveFormComponent.writeValue).toHaveBeenCalledWith('bar'); - }); -}); diff --git a/examples-jest/MockReactiveForms/dependency.component.ts b/examples-jest/MockReactiveForms/dependency.component.ts deleted file mode 100644 index 805a05a6a6..0000000000 --- a/examples-jest/MockReactiveForms/dependency.component.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'dependency-component-selector', - template: `dependency`, -}) -export class DependencyComponent {} diff --git a/examples-jest/MockReactiveForms/tested.component.ts b/examples-jest/MockReactiveForms/tested.component.ts deleted file mode 100644 index 7519e65c9a..0000000000 --- a/examples-jest/MockReactiveForms/tested.component.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Component } from '@angular/core'; -import { FormControl } from '@angular/forms'; - -@Component({ - selector: 'tested', - template: ` `, -}) -export class TestedComponent { - formControl = new FormControl(); -} diff --git a/examples-jest/MockRender/dependency.module.ts b/examples-jest/MockRender/dependency.module.ts deleted file mode 100644 index 22485afbaf..0000000000 --- a/examples-jest/MockRender/dependency.module.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { CommonModule } from '@angular/common'; -import { NgModule } from '@angular/core'; -import { DependencyComponent } from './dependency.component'; - -@NgModule({ - declarations: [DependencyComponent], - imports: [CommonModule], -}) -export class DependencyModule {} diff --git a/examples-jest/MockRender/tested.component.ts b/examples-jest/MockRender/tested.component.ts deleted file mode 100644 index bdc3e1c5af..0000000000 --- a/examples-jest/MockRender/tested.component.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Component, EventEmitter, Input, Output } from '@angular/core'; - -@Component({ - selector: 'tested', - template: ` - - `, -}) -export class TestedComponent { - @Output() - trigger = new EventEmitter(); - - @Input() - value1 = ''; - - @Input() - value2 = ''; -} diff --git a/examples-jest/NG_MOCKS/NG_MOCKS.spec.ts b/examples-jest/NG_MOCKS/NG_MOCKS.spec.ts deleted file mode 100644 index 6c3a7cdf06..0000000000 --- a/examples-jest/NG_MOCKS/NG_MOCKS.spec.ts +++ /dev/null @@ -1,122 +0,0 @@ -import { HttpClientModule } from '@angular/common/http'; -import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { inject, TestBed } from '@angular/core/testing'; -import { isMockedNgDefOf, MockBuilder, NG_MOCKS } from 'ng-mocks'; - -import { - ComponentWeDontWantToMock, - ComponentWeWantToMock, - MyComponent, - MyComponent1, - MyComponent2, - MyComponent3, -} from './fixtures.components'; -import { DirectiveWeDontWantToMock, DirectiveWeWantToMock } from './fixtures.directives'; -import { ModuleWeDontWantToMock, ModuleWeWantToMockBesidesMyModule, MyModule } from './fixtures.modules'; -import { PipeWeDontWantToMock, PipeWeWantToMock, PipeWeWantToRestore } from './fixtures.pipes'; -import { ServiceWeDontWantToMock, ServiceWeWantToCustomize, ServiceWeWantToMock } from './fixtures.services'; -import { - INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK, - INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE, - INJECTION_TOKEN_WE_WANT_TO_MOCK, -} from './fixtures.tokens'; - -describe('NG_MOCKS:deep', () => { - beforeEach(async () => { - const ngModule = MockBuilder(MyComponent, MyModule) - .keep(ModuleWeDontWantToMock) - .keep(ComponentWeDontWantToMock) - .keep(DirectiveWeDontWantToMock) - .keep(PipeWeDontWantToMock) - .keep(ServiceWeDontWantToMock) - .keep(INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK) - - .replace(HttpClientModule, HttpClientTestingModule) - - .mock(ModuleWeWantToMockBesidesMyModule) - .mock(ComponentWeWantToMock) - .mock(DirectiveWeWantToMock) - .mock(PipeWeWantToMock) - .mock(ServiceWeWantToMock) // makes all methods an empty function - .mock(INJECTION_TOKEN_WE_WANT_TO_MOCK) // makes its value undefined - - .mock(ServiceWeWantToCustomize, { prop1: true, getName: () => 'My Customized String' }) - .mock(INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE, 'My_Token') - - // Now the pipe won't be mocked. - .keep(PipeWeWantToRestore) - - // Even it belongs to the module to keep it still will be mocked and replaced. - .mock(MyComponent3) - - // and now we want to build our NgModule. - .build(); - TestBed.configureTestingModule(ngModule); - - // Extra configuration - TestBed.overrideTemplate(MyComponent1, 'If we need to tune testBed'); - TestBed.overrideTemplate(MyComponent2, 'More callbacks'); - - return TestBed.compileComponents(); - }); - - it('should contain mocks', inject([NG_MOCKS], (mocks: Map) => { - // main part - const myComponent = mocks.get(MyComponent); - expect(myComponent).toBe(MyComponent); - const myModule = mocks.get(MyModule); - expect(isMockedNgDefOf(myModule, MyModule, 'm')).toBeTruthy(); - - // keep - const componentWeDontWantToMock = mocks.get(ComponentWeDontWantToMock); - expect(componentWeDontWantToMock).toBe(ComponentWeDontWantToMock); - const directiveWeDontWantToMock = mocks.get(DirectiveWeDontWantToMock); - expect(directiveWeDontWantToMock).toBe(DirectiveWeDontWantToMock); - const pipeWeDontWantToMock = mocks.get(PipeWeDontWantToMock); - expect(pipeWeDontWantToMock).toBe(PipeWeDontWantToMock); - const serviceWeDontWantToMock = mocks.get(ServiceWeDontWantToMock); - expect(serviceWeDontWantToMock).toBe(ServiceWeDontWantToMock); - const injectionTokenWeDontWantToMock = mocks.get(INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK); - expect(injectionTokenWeDontWantToMock).toBe(INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK); - - // replace - const httpClientModule = mocks.get(HttpClientModule); - expect(httpClientModule).toBe(HttpClientTestingModule); - - // mock - const moduleWeWantToMockBesidesMyModule = mocks.get(ModuleWeWantToMockBesidesMyModule); - expect(isMockedNgDefOf(moduleWeWantToMockBesidesMyModule, ModuleWeWantToMockBesidesMyModule, 'm')).toBeTruthy(); - const componentWeWantToMock = mocks.get(ComponentWeWantToMock); - expect(isMockedNgDefOf(componentWeWantToMock, ComponentWeWantToMock, 'c')).toBeTruthy(); - const directiveWeWantToMock = mocks.get(DirectiveWeWantToMock); - expect(isMockedNgDefOf(directiveWeWantToMock, DirectiveWeWantToMock, 'd')).toBeTruthy(); - const pipeWeWantToMock = mocks.get(PipeWeWantToMock); - expect(isMockedNgDefOf(pipeWeWantToMock, PipeWeWantToMock, 'p')).toBeTruthy(); - const serviceWeWantToMock = mocks.get(ServiceWeWantToMock); - expect(serviceWeWantToMock).toBeDefined(); - expect(serviceWeWantToMock.useValue).toBeDefined(); - expect(serviceWeWantToMock.useValue.getName).toBeDefined(); - expect(serviceWeWantToMock.useValue.getName()).toBeUndefined(); - expect(mocks.has(INJECTION_TOKEN_WE_WANT_TO_MOCK)).toBeDefined(); - expect(mocks.get(INJECTION_TOKEN_WE_WANT_TO_MOCK)).toBeUndefined(); - - // customize - const serviceWeWantToCustomize = mocks.get(ServiceWeWantToCustomize); - expect(serviceWeWantToCustomize).toBeDefined(); - expect(serviceWeWantToCustomize.useValue).toBeDefined(); - expect(serviceWeWantToCustomize.useValue.getName).toBeDefined(); - expect(serviceWeWantToCustomize.useValue.getName()).toEqual('My Customized String'); - expect(serviceWeWantToCustomize.useValue.prop1).toEqual(true); - const injectionTokenWeWantToCustomize = mocks.get(INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE); - expect(injectionTokenWeWantToCustomize).toBeDefined(); - expect(injectionTokenWeWantToCustomize.useValue).toEqual('My_Token'); - - // restore - const pipeWeWantToRestore = mocks.get(PipeWeWantToRestore); - expect(pipeWeWantToRestore).toBe(PipeWeWantToRestore); - - // mock nested - const myComponent3 = mocks.get(MyComponent3); - expect(isMockedNgDefOf(myComponent3, MyComponent3, 'c')).toBeTruthy(); - })); -}); diff --git a/examples-jest/NG_MOCKS/fixtures.directives.ts b/examples-jest/NG_MOCKS/fixtures.directives.ts deleted file mode 100644 index 6dbb2e86f8..0000000000 --- a/examples-jest/NG_MOCKS/fixtures.directives.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Directive } from '@angular/core'; - -@Directive({ - selector: 'MyDirective', -}) -export class MyDirective {} - -@Directive({ - selector: 'WeDontWantToMock', -}) -export class DirectiveWeDontWantToMock {} - -@Directive({ - selector: '[WeWantToMock]', -}) -export class DirectiveWeWantToMock {} diff --git a/examples-jest/NG_MOCKS/fixtures.modules.ts b/examples-jest/NG_MOCKS/fixtures.modules.ts deleted file mode 100644 index 264824c22b..0000000000 --- a/examples-jest/NG_MOCKS/fixtures.modules.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { HttpClientModule } from '@angular/common/http'; -import { NgModule } from '@angular/core'; - -import { - ComponentContentChild, - ComponentWeDontWantToMock, - ComponentWeWantToMock, - MyComponent, - MyComponent1, - MyComponent2, - MyComponent3, -} from './fixtures.components'; -import { DirectiveWeDontWantToMock, DirectiveWeWantToMock, MyDirective } from './fixtures.directives'; -import { - MyPipe, - PipeWeDontWantToMock, - PipeWeWantToCustomize, - PipeWeWantToMock, - PipeWeWantToRestore, -} from './fixtures.pipes'; -import { - MyService1, - MyService2, - ServiceWeDontWantToMock, - ServiceWeWantToCustomize, - ServiceWeWantToMock, -} from './fixtures.services'; -import { - INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK, - INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE, - INJECTION_TOKEN_WE_WANT_TO_MOCK, -} from './fixtures.tokens'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - declarations: [ - ComponentWeDontWantToMock, - ComponentWeWantToMock, - DirectiveWeDontWantToMock, - DirectiveWeWantToMock, - PipeWeDontWantToMock, - PipeWeWantToMock, - PipeWeWantToCustomize, - PipeWeWantToRestore, - ], - exports: [ - ComponentWeDontWantToMock, - ComponentWeWantToMock, - DirectiveWeDontWantToMock, - DirectiveWeWantToMock, - PipeWeDontWantToMock, - PipeWeWantToMock, - PipeWeWantToCustomize, - PipeWeWantToRestore, - ], - providers: [ - ServiceWeDontWantToMock, - ServiceWeWantToMock, - { - provide: INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK, - useValue: 'INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK', - }, - { - provide: INJECTION_TOKEN_WE_WANT_TO_MOCK, - useValue: 'INJECTION_TOKEN_WE_WANT_TO_MOCK', - }, - { - provide: INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE, - useValue: 'INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE', - }, - ], -}) -export class ModuleWeWantToMockBesidesMyModule {} - -@NgModule({ - declarations: [MyComponent1, MyComponent2, MyComponent3, ComponentContentChild], - exports: [MyComponent1, MyComponent2, MyComponent3, ComponentContentChild], - imports: [CommonModule], -}) -export class ModuleWeDontWantToMock {} - -@NgModule({ - declarations: [MyComponent, MyDirective, MyPipe], - exports: [MyComponent, MyDirective, MyPipe], - imports: [HttpClientModule, ModuleWeWantToMockBesidesMyModule, ModuleWeDontWantToMock], - providers: [MyService1, MyService2, ServiceWeWantToCustomize], -}) -export class MyModule {} diff --git a/examples-jest/NG_MOCKS/fixtures.pipes.ts b/examples-jest/NG_MOCKS/fixtures.pipes.ts deleted file mode 100644 index a1034148cb..0000000000 --- a/examples-jest/NG_MOCKS/fixtures.pipes.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { Pipe, PipeTransform } from '@angular/core'; - -@Pipe({ - name: 'MyPipe', -}) -export class MyPipe implements PipeTransform { - protected prefix = 'MyPipe:'; - - public transform(value: any, ...args: any[]): any { - return this.prefix + value; - } -} - -@Pipe({ - name: 'PipeWeDontWantToMock', -}) -export class PipeWeDontWantToMock implements PipeTransform { - protected prefix = 'PipeWeDontWantToMock:'; - - public transform(value: any, ...args: any[]): any { - return this.prefix + value; - } -} - -@Pipe({ - name: 'PipeWeWantToMock', -}) -export class PipeWeWantToMock implements PipeTransform { - protected prefix = 'PipeWeWantToMock:'; - - public transform(value: any, ...args: any[]): any { - return this.prefix + value; - } -} - -@Pipe({ - name: 'PipeWeWantToCustomize', -}) -export class PipeWeWantToCustomize implements PipeTransform { - protected prefix = 'PipeWeWantToCustomize:'; - - public transform(value: any, ...args: any[]): any { - return this.prefix + value; - } -} - -@Pipe({ - name: 'PipeWeWantToRestore', -}) -export class PipeWeWantToRestore implements PipeTransform { - protected prefix = 'PipeWeWantToRestore:'; - - public transform(value: any, ...args: any[]): any { - return this.prefix + value; - } -} diff --git a/examples-jest/NG_MOCKS/fixtures.services.ts b/examples-jest/NG_MOCKS/fixtures.services.ts deleted file mode 100644 index 4fb36c910a..0000000000 --- a/examples-jest/NG_MOCKS/fixtures.services.ts +++ /dev/null @@ -1,100 +0,0 @@ -import { Injectable } from '@angular/core'; - -@Injectable() -export class MyService1 { - protected value = 'MyService1'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class MyService2 { - protected value = 'MyService2'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class ServiceWeDontWantToMock { - protected value = 'ServiceWeDontWantToMock'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class ServiceWeWantToMock { - protected value = 'ServiceWeWantToMock'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class ServiceWeWantToCustomize { - protected value = 'ServiceWeWantToCustomize'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class AnythingWeWant1 { - protected value = 'AnythingWeWant1'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class TheSameAsAnyProvider { - protected value = 'TheSameAsAnyProvider'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class AnythingWeWant2 { - protected value = 'AnythingWeWant2'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class MyCustomProvider1 { - protected value = 'MyCustomProvider1'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class MyCustomProvider2 { - protected value = 'MyCustomProvider2'; - - public getName() { - return this.value; - } -} - -@Injectable() -export class MyCustomProvider3 { - protected value = 'MyCustomProvider3'; - - public getName() { - return this.value; - } -} diff --git a/examples-jest/NG_MOCKS/fixtures.tokens.ts b/examples-jest/NG_MOCKS/fixtures.tokens.ts deleted file mode 100644 index 8d527302f7..0000000000 --- a/examples-jest/NG_MOCKS/fixtures.tokens.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { InjectionToken } from '@angular/core'; - -export const INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK = new InjectionToken('INJECTION_TOKEN_WE_DONT_WANT_TO_MOCK'); - -export const INJECTION_TOKEN_WE_WANT_TO_MOCK = new InjectionToken('INJECTION_TOKEN_WE_WANT_TO_MOCK'); - -export const INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE = new InjectionToken('INJECTION_TOKEN_WE_WANT_TO_CUSTOMIZE'); diff --git a/examples-jasmine/MockBuilder/MockBuilder.spec.ts b/examples/MockBuilder/MockBuilder.spec.ts similarity index 100% rename from examples-jasmine/MockBuilder/MockBuilder.spec.ts rename to examples/MockBuilder/MockBuilder.spec.ts diff --git a/examples-jest/MockBuilder/fixtures.components.ts b/examples/MockBuilder/fixtures.components.ts similarity index 99% rename from examples-jest/MockBuilder/fixtures.components.ts rename to examples/MockBuilder/fixtures.components.ts index 4427415110..93ee3c442b 100644 --- a/examples-jest/MockBuilder/fixtures.components.ts +++ b/examples/MockBuilder/fixtures.components.ts @@ -1,6 +1,6 @@ import { Component, ContentChild, Inject, Input, Optional, TemplateRef } from '@angular/core'; -import { staticFalse } from '../../tests-jest'; +import { staticFalse } from '../../tests'; import { AnythingWeWant1, diff --git a/examples-jasmine/MockBuilder/fixtures.directives.ts b/examples/MockBuilder/fixtures.directives.ts similarity index 100% rename from examples-jasmine/MockBuilder/fixtures.directives.ts rename to examples/MockBuilder/fixtures.directives.ts diff --git a/examples-jasmine/MockBuilder/fixtures.modules.ts b/examples/MockBuilder/fixtures.modules.ts similarity index 100% rename from examples-jasmine/MockBuilder/fixtures.modules.ts rename to examples/MockBuilder/fixtures.modules.ts diff --git a/examples-jasmine/MockBuilder/fixtures.pipes.ts b/examples/MockBuilder/fixtures.pipes.ts similarity index 100% rename from examples-jasmine/MockBuilder/fixtures.pipes.ts rename to examples/MockBuilder/fixtures.pipes.ts diff --git a/examples-jasmine/MockBuilder/fixtures.services.ts b/examples/MockBuilder/fixtures.services.ts similarity index 100% rename from examples-jasmine/MockBuilder/fixtures.services.ts rename to examples/MockBuilder/fixtures.services.ts diff --git a/examples-jasmine/MockBuilder/fixtures.tokens.ts b/examples/MockBuilder/fixtures.tokens.ts similarity index 100% rename from examples-jasmine/MockBuilder/fixtures.tokens.ts rename to examples/MockBuilder/fixtures.tokens.ts diff --git a/examples-jasmine/MockComponent/MockComponent.spec.ts b/examples/MockComponent/MockComponent.spec.ts similarity index 100% rename from examples-jasmine/MockComponent/MockComponent.spec.ts rename to examples/MockComponent/MockComponent.spec.ts diff --git a/examples-jest/MockModule/dependency.component.ts b/examples/MockComponent/dependency.component.ts similarity index 89% rename from examples-jest/MockModule/dependency.component.ts rename to examples/MockComponent/dependency.component.ts index 2b9dccf184..e5ed125ccb 100644 --- a/examples-jest/MockModule/dependency.component.ts +++ b/examples/MockComponent/dependency.component.ts @@ -1,6 +1,6 @@ import { Component, ContentChild, EventEmitter, Input, Output, TemplateRef } from '@angular/core'; -import { staticFalse } from '../../tests-jest'; +import { staticFalse } from '../../tests'; @Component({ selector: 'dependency-component-selector', diff --git a/examples-jasmine/MockComponent/tested.component.ts b/examples/MockComponent/tested.component.ts similarity index 100% rename from examples-jasmine/MockComponent/tested.component.ts rename to examples/MockComponent/tested.component.ts diff --git a/examples-jasmine/MockDirective-Attribute/MockDirective.spec.ts b/examples/MockDirective-Attribute/MockDirective.spec.ts similarity index 100% rename from examples-jasmine/MockDirective-Attribute/MockDirective.spec.ts rename to examples/MockDirective-Attribute/MockDirective.spec.ts diff --git a/examples-jasmine/MockDirective-Attribute/dependency.directive.ts b/examples/MockDirective-Attribute/dependency.directive.ts similarity index 100% rename from examples-jasmine/MockDirective-Attribute/dependency.directive.ts rename to examples/MockDirective-Attribute/dependency.directive.ts diff --git a/examples-jasmine/MockDirective-Attribute/tested.component.ts b/examples/MockDirective-Attribute/tested.component.ts similarity index 100% rename from examples-jasmine/MockDirective-Attribute/tested.component.ts rename to examples/MockDirective-Attribute/tested.component.ts diff --git a/examples-jasmine/MockDirective-Structural/MockDirective.spec.ts b/examples/MockDirective-Structural/MockDirective.spec.ts similarity index 100% rename from examples-jasmine/MockDirective-Structural/MockDirective.spec.ts rename to examples/MockDirective-Structural/MockDirective.spec.ts diff --git a/examples-jasmine/MockDirective-Structural/dependency.directive.ts b/examples/MockDirective-Structural/dependency.directive.ts similarity index 100% rename from examples-jasmine/MockDirective-Structural/dependency.directive.ts rename to examples/MockDirective-Structural/dependency.directive.ts diff --git a/examples-jasmine/MockDirective-Structural/tested.component.ts b/examples/MockDirective-Structural/tested.component.ts similarity index 100% rename from examples-jasmine/MockDirective-Structural/tested.component.ts rename to examples/MockDirective-Structural/tested.component.ts diff --git a/examples-jasmine/MockModule/MockModule.spec.ts b/examples/MockModule/MockModule.spec.ts similarity index 100% rename from examples-jasmine/MockModule/MockModule.spec.ts rename to examples/MockModule/MockModule.spec.ts diff --git a/examples-jest/MockRender/dependency.component.ts b/examples/MockModule/dependency.component.ts similarity index 89% rename from examples-jest/MockRender/dependency.component.ts rename to examples/MockModule/dependency.component.ts index 2b9dccf184..e5ed125ccb 100644 --- a/examples-jest/MockRender/dependency.component.ts +++ b/examples/MockModule/dependency.component.ts @@ -1,6 +1,6 @@ import { Component, ContentChild, EventEmitter, Input, Output, TemplateRef } from '@angular/core'; -import { staticFalse } from '../../tests-jest'; +import { staticFalse } from '../../tests'; @Component({ selector: 'dependency-component-selector', diff --git a/examples-jasmine/MockModule/dependency.module.ts b/examples/MockModule/dependency.module.ts similarity index 100% rename from examples-jasmine/MockModule/dependency.module.ts rename to examples/MockModule/dependency.module.ts diff --git a/examples-jasmine/MockModule/tested.component.ts b/examples/MockModule/tested.component.ts similarity index 100% rename from examples-jasmine/MockModule/tested.component.ts rename to examples/MockModule/tested.component.ts diff --git a/examples-jasmine/MockPipe/MockPipe.spec.ts b/examples/MockPipe/MockPipe.spec.ts similarity index 100% rename from examples-jasmine/MockPipe/MockPipe.spec.ts rename to examples/MockPipe/MockPipe.spec.ts diff --git a/examples-jasmine/MockPipe/dependency.pipe.ts b/examples/MockPipe/dependency.pipe.ts similarity index 100% rename from examples-jasmine/MockPipe/dependency.pipe.ts rename to examples/MockPipe/dependency.pipe.ts diff --git a/examples-jasmine/MockPipe/tested.component.ts b/examples/MockPipe/tested.component.ts similarity index 100% rename from examples-jasmine/MockPipe/tested.component.ts rename to examples/MockPipe/tested.component.ts diff --git a/examples-jasmine/MockReactiveForms/MockReactiveForms.spec.ts b/examples/MockReactiveForms/MockReactiveForms.spec.ts similarity index 100% rename from examples-jasmine/MockReactiveForms/MockReactiveForms.spec.ts rename to examples/MockReactiveForms/MockReactiveForms.spec.ts diff --git a/examples-jasmine/MockReactiveForms/dependency.component.ts b/examples/MockReactiveForms/dependency.component.ts similarity index 100% rename from examples-jasmine/MockReactiveForms/dependency.component.ts rename to examples/MockReactiveForms/dependency.component.ts diff --git a/examples-jasmine/MockReactiveForms/tested.component.ts b/examples/MockReactiveForms/tested.component.ts similarity index 100% rename from examples-jasmine/MockReactiveForms/tested.component.ts rename to examples/MockReactiveForms/tested.component.ts diff --git a/examples-jest/MockRender/MockRender.spec.ts b/examples/MockRender/MockRender.spec.ts similarity index 89% rename from examples-jest/MockRender/MockRender.spec.ts rename to examples/MockRender/MockRender.spec.ts index cc42795e8b..c815d03c58 100644 --- a/examples-jest/MockRender/MockRender.spec.ts +++ b/examples/MockRender/MockRender.spec.ts @@ -4,6 +4,10 @@ import { MockModule, MockRender, ngMocks } from 'ng-mocks'; import { DependencyModule } from './dependency.module'; import { TestedComponent } from './tested.component'; +// fix to support both jasmine and jest in the test +declare let jest: any; +declare let jasmine: any; + describe('MockRender', () => { beforeEach(() => { TestBed.configureTestingModule({ @@ -13,7 +17,7 @@ describe('MockRender', () => { }); it('renders template', () => { - const spy = jest.fn(); + const spy = typeof jest !== 'undefined' ? jest.fn() : jasmine.createSpy(); const fixture = MockRender( ` @@ -39,7 +43,7 @@ describe('MockRender', () => { }); it('renders component', () => { - const spy = jest.fn(); + const spy = typeof jest !== 'undefined' ? jest.fn() : jasmine.createSpy(); // generates template like: // // and returns fixture with a component with properties value1, value2 and empty callback trigger. diff --git a/examples-jest/MockComponent/dependency.component.ts b/examples/MockRender/dependency.component.ts similarity index 89% rename from examples-jest/MockComponent/dependency.component.ts rename to examples/MockRender/dependency.component.ts index 2b9dccf184..e5ed125ccb 100644 --- a/examples-jest/MockComponent/dependency.component.ts +++ b/examples/MockRender/dependency.component.ts @@ -1,6 +1,6 @@ import { Component, ContentChild, EventEmitter, Input, Output, TemplateRef } from '@angular/core'; -import { staticFalse } from '../../tests-jest'; +import { staticFalse } from '../../tests'; @Component({ selector: 'dependency-component-selector', diff --git a/examples-jasmine/MockRender/dependency.module.ts b/examples/MockRender/dependency.module.ts similarity index 100% rename from examples-jasmine/MockRender/dependency.module.ts rename to examples/MockRender/dependency.module.ts diff --git a/examples-jasmine/MockRender/tested.component.ts b/examples/MockRender/tested.component.ts similarity index 100% rename from examples-jasmine/MockRender/tested.component.ts rename to examples/MockRender/tested.component.ts diff --git a/examples-jasmine/NG_MOCKS/NG_MOCKS.spec.ts b/examples/NG_MOCKS/NG_MOCKS.spec.ts similarity index 100% rename from examples-jasmine/NG_MOCKS/NG_MOCKS.spec.ts rename to examples/NG_MOCKS/NG_MOCKS.spec.ts diff --git a/examples-jest/NG_MOCKS/fixtures.components.ts b/examples/NG_MOCKS/fixtures.components.ts similarity index 98% rename from examples-jest/NG_MOCKS/fixtures.components.ts rename to examples/NG_MOCKS/fixtures.components.ts index a407350e61..b15d870dec 100644 --- a/examples-jest/NG_MOCKS/fixtures.components.ts +++ b/examples/NG_MOCKS/fixtures.components.ts @@ -1,6 +1,6 @@ import { Component, ContentChild, Inject, Input, Optional, TemplateRef } from '@angular/core'; -import { staticFalse } from '../../tests-jest'; +import { staticFalse } from '../../tests'; import { AnythingWeWant1, diff --git a/examples-jasmine/NG_MOCKS/fixtures.directives.ts b/examples/NG_MOCKS/fixtures.directives.ts similarity index 100% rename from examples-jasmine/NG_MOCKS/fixtures.directives.ts rename to examples/NG_MOCKS/fixtures.directives.ts diff --git a/examples-jasmine/NG_MOCKS/fixtures.modules.ts b/examples/NG_MOCKS/fixtures.modules.ts similarity index 100% rename from examples-jasmine/NG_MOCKS/fixtures.modules.ts rename to examples/NG_MOCKS/fixtures.modules.ts diff --git a/examples-jasmine/NG_MOCKS/fixtures.pipes.ts b/examples/NG_MOCKS/fixtures.pipes.ts similarity index 100% rename from examples-jasmine/NG_MOCKS/fixtures.pipes.ts rename to examples/NG_MOCKS/fixtures.pipes.ts diff --git a/examples-jasmine/NG_MOCKS/fixtures.services.ts b/examples/NG_MOCKS/fixtures.services.ts similarity index 100% rename from examples-jasmine/NG_MOCKS/fixtures.services.ts rename to examples/NG_MOCKS/fixtures.services.ts diff --git a/examples-jasmine/NG_MOCKS/fixtures.tokens.ts b/examples/NG_MOCKS/fixtures.tokens.ts similarity index 100% rename from examples-jasmine/NG_MOCKS/fixtures.tokens.ts rename to examples/NG_MOCKS/fixtures.tokens.ts diff --git a/karma.conf.ts b/karma.conf.ts index 0ec46bfae3..2554288919 100644 --- a/karma.conf.ts +++ b/karma.conf.ts @@ -24,8 +24,8 @@ module.exports = (config: any) => { 'index.ts', 'jasmine.ts', { pattern: 'lib/**/*.ts' }, - { pattern: 'examples-jasmine/**/*.ts' }, - { pattern: 'tests-jasmine/**/*.ts' }, + { pattern: 'examples/**/*.ts' }, + { pattern: 'tests/**/*.ts' }, ], frameworks: ['jasmine', 'karma-typescript'], logLevel: config.LOG_INFO, @@ -37,7 +37,7 @@ module.exports = (config: any) => { singleRun: true, karmaTypescriptConfig: { - include: ['karma-test-shim.ts', 'examples-jasmine/**/*', 'lib/**/*', 'tests-jasmine/**/*'], + include: ['karma-test-shim.ts', 'lib/**/*', 'examples/**/*', 'tests/**/*'], tsconfig: 'tsconfig.json', }, }); diff --git a/lib/mock-component/mock-component.spec.ts b/lib/mock-component/mock-component.spec.ts index cc21e5dbfb..459db26905 100644 --- a/lib/mock-component/mock-component.spec.ts +++ b/lib/mock-component/mock-component.spec.ts @@ -12,7 +12,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms'; import { By } from '@angular/platform-browser'; -import { staticTrue } from '../../tests-jasmine'; +import { staticTrue } from '../../tests'; import { MockedDirective } from '../mock-directive'; import { ngMocks } from '../mock-helper'; diff --git a/lib/mock-component/test-components/template-outlet.component.ts b/lib/mock-component/test-components/template-outlet.component.ts index f56a5e4b46..b03260eb92 100644 --- a/lib/mock-component/test-components/template-outlet.component.ts +++ b/lib/mock-component/test-components/template-outlet.component.ts @@ -1,6 +1,6 @@ import { Component, ContentChild, ElementRef, TemplateRef, ViewChild } from '@angular/core'; -import { staticFalse } from '../../../tests-jasmine'; +import { staticFalse } from '../../../tests'; @Component({ selector: 'template-outlet-component', diff --git a/lib/mock-directive/mock-directive.spec.ts b/lib/mock-directive/mock-directive.spec.ts index a2fba1fea4..01b7ff822a 100644 --- a/lib/mock-directive/mock-directive.spec.ts +++ b/lib/mock-directive/mock-directive.spec.ts @@ -15,7 +15,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { FormControl, FormControlDirective } from '@angular/forms'; import { By } from '@angular/platform-browser'; -import { staticFalse } from '../../tests-jasmine'; +import { staticFalse } from '../../tests'; import { ngMocks } from '../mock-helper'; import { MockDirective, MockedDirective } from './mock-directive'; diff --git a/package.json b/package.json index a9c65976d5..56615a53bd 100644 --- a/package.json +++ b/package.json @@ -49,21 +49,21 @@ "s:a9j": "P=e2e/angular9-jest/node_modules/ng-mocks && rm -Rf $P && mkdir -p $P && cp -R dist $P && cp package.json $P", "s:test": "npm run s:test:a5 && npm run s:test:a6 && npm run s:test:a7 && npm run s:test:a8 && npm run s:test:a9", "s:test:a5": "npm run s:test:a5ja && npm run s:test:a5je", - "s:test:a5ja": "P=e2e/angular5/src/test && rm -Rf $P && mkdir -p $P && cp -R tests-jasmine $P && cp -R examples-jasmine $P", - "s:test:a5je": "P=e2e/angular5-jest/src/test && rm -Rf $P && mkdir -p $P && cp -R tests-jest $P && cp -R examples-jest $P", + "s:test:a5ja": "P=e2e/angular5/src/test && rm -Rf $P && mkdir -p $P && cp -R tests $P && cp -R examples $P", + "s:test:a5je": "P=e2e/angular5-jest/src/test && rm -Rf $P && mkdir -p $P && cp -R tests $P && cp -R examples $P", "s:test:a6": "npm run s:test:a6ja && npm run s:test:a6je", - "s:test:a6ja": "P=e2e/angular6/src/test && rm -Rf $P && mkdir -p $P && cp -R tests-jasmine $P && cp -R examples-jasmine $P", - "s:test:a6je": "P=e2e/angular6-jest/src/test && rm -Rf $P && mkdir -p $P && cp -R tests-jest $P && cp -R examples-jest $P", + "s:test:a6ja": "P=e2e/angular6/src/test && rm -Rf $P && mkdir -p $P && cp -R tests $P && cp -R examples $P", + "s:test:a6je": "P=e2e/angular6-jest/src/test && rm -Rf $P && mkdir -p $P && cp -R tests $P && cp -R examples $P", "s:test:a7": "npm run s:test:a7ja && npm run s:test:a7je", - "s:test:a7ja": "P=e2e/angular7/src/test && rm -Rf $P && mkdir -p $P && cp -R tests-jasmine $P && cp -R examples-jasmine $P", - "s:test:a7je": "P=e2e/angular7-jest/src/test && rm -Rf $P && mkdir -p $P && cp -R tests-jest $P && cp -R examples-jest $P", + "s:test:a7ja": "P=e2e/angular7/src/test && rm -Rf $P && mkdir -p $P && cp -R tests $P && cp -R examples $P", + "s:test:a7je": "P=e2e/angular7-jest/src/test && rm -Rf $P && mkdir -p $P && cp -R tests $P && cp -R examples $P", "s:test:a8": "npm run s:test:a8ja && npm run s:test:a8je", - "s:test:a8ja": "P=e2e/angular8/src/test && rm -Rf $P && mkdir -p $P && cp -R tests-jasmine $P && cp -R examples-jasmine $P", - "s:test:a8je": "P=e2e/angular8-jest/src/test && rm -Rf $P && mkdir -p $P && cp -R tests-jest $P && cp -R examples-jest $P", + "s:test:a8ja": "P=e2e/angular8/src/test && rm -Rf $P && mkdir -p $P && cp -R tests $P && cp -R examples $P", + "s:test:a8je": "P=e2e/angular8-jest/src/test && rm -Rf $P && mkdir -p $P && cp -R tests $P && cp -R examples $P", "s:test:a9": "npm run s:test:a9f && npm run s:test:a9t && npm run s:test:a9j", - "s:test:a9f": "P=e2e/angular9-ivy-false/src/test && rm -Rf $P && mkdir -p $P && cp -R tests-jasmine $P && cp -R examples-jasmine $P", - "s:test:a9t": "P=e2e/angular9-ivy-true/src/test && rm -Rf $P && mkdir -p $P && cp -R tests-jasmine $P && cp -R examples-jasmine $P", - "s:test:a9j": "P=e2e/angular9-jest/src/test && rm -Rf $P && mkdir -p $P && cp -R tests-jest $P && cp -R examples-jest $P", + "s:test:a9f": "P=e2e/angular9-ivy-false/src/test && rm -Rf $P && mkdir -p $P && cp -R tests $P && cp -R examples $P", + "s:test:a9t": "P=e2e/angular9-ivy-true/src/test && rm -Rf $P && mkdir -p $P && cp -R tests $P && cp -R examples $P", + "s:test:a9j": "P=e2e/angular9-jest/src/test && rm -Rf $P && mkdir -p $P && cp -R tests $P && cp -R examples $P", "e2e": "npm run build && npm run i:a && npm run s:a && npm run s:test && npm run test:a", "clean": "rm -rf dist coverage", "commit": "npx git-cz", diff --git a/tests-jasmine/context-with-directives/context-with-directives.spec.ts b/tests-jasmine/context-with-directives/context-with-directives.spec.ts deleted file mode 100644 index c79db7edcb..0000000000 --- a/tests-jasmine/context-with-directives/context-with-directives.spec.ts +++ /dev/null @@ -1,110 +0,0 @@ -import { CommonModule } from '@angular/common'; -import { TestBed } from '@angular/core/testing'; -import { MockDirective, MockRender, ngMocks } from 'ng-mocks'; - -import { CustomRootComponent } from './custom-root.component'; -import { CustomTypeDirective } from './custom-type.directive'; - -describe('context-with-directives:real', () => { - beforeEach(done => { - TestBed.configureTestingModule({ - declarations: [CustomTypeDirective, CustomRootComponent], - imports: [CommonModule], - }) - .compileComponents() - .then(done); - }); - - it('renders everything right', () => { - const fixture = MockRender(` - -
header
- - template w/ directive w/o binding - - - template w/ directive w/ binding {{ value[0] }} - - - template w/ directive w/ binding w/o render - - - template w/o directive w/o binding - - - template w/o directive w/ binding {{ value[0] }} - -
footer
-
- `); - - // template should be rendered under .template - expect(ngMocks.find(fixture.debugElement, '.template').nativeElement.innerHTML.replace(/\s+/gm, ' ')).toContain( - ' template w/ directive w/o binding ' - ); - - // template1 should be rendered under .template1 - expect(ngMocks.find(fixture.debugElement, '.template1').nativeElement.innerHTML.replace(/\s+/gm, ' ')).toContain( - ' template w/ directive w/ binding 1 ' - ); - - // template2 should not be rendered - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).not.toContain( - ' template w/ directive w/ binding w/o render ' - ); - - // unused ng-templates shouldn't be rendered at all - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).not.toContain(' template w/o directive w/o binding '); - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).not.toContain(' template w/o directive w/ binding '); - - // ng-content contains header and footer - expect(ngMocks.find(fixture.debugElement, '.nested').nativeElement.innerHTML.replace(/\s+/, ' ')).toContain( - '
header
' - ); - expect(ngMocks.find(fixture.debugElement, '.nested').nativeElement.innerHTML.replace(/\s+/, ' ')).toContain( - '
footer
' - ); - }); -}); - -describe('context-with-directives:mock', () => { - beforeEach(done => { - TestBed.configureTestingModule({ - declarations: [MockDirective(CustomTypeDirective), MockDirective(CustomRootComponent)], - }) - .compileComponents() - .then(done); - }); - - it('renders everything what is not template', () => { - const fixture = MockRender(` - -
header
- - template w/ directive w/o binding - - - template w/ directive w/ binding {{ value[0] }} - - - template w/ directive w/ binding {{ value[0] }} - - - template w/o directive w/o binding - - - template w/o directive w/ binding {{ value[0] }} - -
footer
-
- `); - - expect(fixture.nativeElement.innerHTML).toContain('
header
'); - expect(fixture.nativeElement.innerHTML).toContain('
footer
'); - - // No templates should be rendered when we mock them. - // The reason for that is that only directive knows when to render it, that means if we want to render, - // we should do that manually. - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).not.toContain(' template '); - }); -}); diff --git a/tests-jasmine/control-value-accessor-form-control/test.spec.ts b/tests-jasmine/control-value-accessor-form-control/test.spec.ts deleted file mode 100644 index 562ffc7c30..0000000000 --- a/tests-jasmine/control-value-accessor-form-control/test.spec.ts +++ /dev/null @@ -1,107 +0,0 @@ -import { ReactiveFormsModule } from '@angular/forms'; -import { MockBuilder, MockComponent, MockHelper, MockRender } from 'ng-mocks'; - -import { ControlComponent, TargetComponent, TargetModule } from './fixtures'; - -// a real case to check possible behavior. -describe('control-value-accessor-form-control:real', () => { - beforeEach(() => MockBuilder(TargetComponent).keep(TargetModule)); - - it('respects our formControl', () => { - const fixture = MockRender(TargetComponent, {}, false); - const mock = MockHelper.findOrFail(fixture.debugElement, ControlComponent).componentInstance; - spyOn(mock, 'writeValue').and.callThrough(); - spyOn(mock, 'setDisabledState').and.callThrough(); - fixture.detectChanges(); - - expect(mock.writeValue).toHaveBeenCalledWith(null); - expect(mock.setDisabledState).not.toHaveBeenCalled(); - expect(fixture.point.componentInstance.control.touched).toBeFalsy(); - - // checking via original component - fixture.point.componentInstance.control.setValue('test1'); - expect(mock.writeValue).toHaveBeenCalledWith('test1'); - expect(fixture.point.componentInstance.control.touched).toBeFalsy(); - - fixture.point.componentInstance.control.setValue('test2'); - expect(mock.writeValue).toHaveBeenCalledWith('test2'); - expect(fixture.point.componentInstance.control.touched).toBeFalsy(); - - // checking that touch works - mock.changeTouch(); - expect(fixture.point.componentInstance.control.touched).toBeTruthy(); - - // checking that reset works - fixture.point.componentInstance.control.markAsUntouched(); - expect(fixture.point.componentInstance.control.touched).toBeFalsy(); - - // checking that disabled works - fixture.point.componentInstance.control.disable(); - expect(mock.setDisabledState).toHaveBeenCalledWith(true); - fixture.point.componentInstance.control.enable(); - expect(mock.setDisabledState).toHaveBeenCalledWith(false); - - // changeValue doesn't trigger anything else but the callback. Therefore it doesn't render new value. - // It only updates the original control's value. - mock.changeValue('test3'); - expect(mock.writeValue).not.toHaveBeenCalledWith('test3'); - expect(fixture.point.componentInstance.control.touched).toBeFalsy(); - expect(fixture.point.componentInstance.control.value).toBe('test3'); - }); -}); - -// a way that ensures that a mocked component behaves the same way as real one. -describe('control-value-accessor-form-control:mock', () => { - beforeEach(() => MockBuilder(TargetComponent, TargetModule).keep(ReactiveFormsModule)); - - it('respects our formControl', () => { - const fixture = MockRender(TargetComponent, {}, false); - const mock = MockHelper.findOrFail(fixture.debugElement, MockComponent(ControlComponent)).componentInstance; - spyOn(mock, 'writeValue').and.callThrough(); - spyOn(mock, 'setDisabledState').and.callThrough(); - spyOn(mock, 'registerOnChange').and.callThrough(); - spyOn(mock, 'registerOnTouched').and.callThrough(); - fixture.detectChanges(); - - expect(mock.writeValue).toHaveBeenCalledWith(null); - expect(mock.setDisabledState).not.toHaveBeenCalled(); - expect(fixture.point.componentInstance.control.touched).toBeFalsy(); - - // checking via original component - fixture.point.componentInstance.control.setValue('test1'); - expect(mock.writeValue).toHaveBeenCalledWith('test1'); - expect(fixture.point.componentInstance.control.touched).toBeFalsy(); - - fixture.point.componentInstance.control.setValue('test2'); - expect(mock.writeValue).toHaveBeenCalledWith('test2'); - expect(fixture.point.componentInstance.control.touched).toBeFalsy(); - - // checking that touch works - mock.__simulateTouch(); - expect(fixture.point.componentInstance.control.touched).toBeTruthy(); - fixture.point.componentInstance.control.markAsUntouched(); - expect(fixture.point.componentInstance.control.touched).toBeFalsy(); - // a way through a spy - MockHelper.mockService(mock, 'registerOnTouched').calls.first().args[0](); - expect(fixture.point.componentInstance.control.touched).toBeTruthy(); - fixture.point.componentInstance.control.markAsUntouched(); - - // checking that disabled works - fixture.point.componentInstance.control.disable(); - expect(mock.setDisabledState).toHaveBeenCalledWith(true); - fixture.point.componentInstance.control.enable(); - expect(mock.setDisabledState).toHaveBeenCalledWith(false); - - // changeValue doesn't trigger anything else but the callback. Therefore it doesn't render new value. - // It only updates the original control's value. - mock.__simulateChange('test3'); - expect(mock.writeValue).not.toHaveBeenCalledWith('test3'); - expect(fixture.point.componentInstance.control.touched).toBeFalsy(); - expect(fixture.point.componentInstance.control.value).toBe('test3'); - // a way through a spy - MockHelper.mockService(mock, 'registerOnChange').calls.first().args[0]('test4'); - expect(mock.writeValue).not.toHaveBeenCalledWith('test4'); - expect(fixture.point.componentInstance.control.touched).toBeFalsy(); - expect(fixture.point.componentInstance.control.value).toBe('test4'); - }); -}); diff --git a/tests-jasmine/exports-only/test.spec.ts b/tests-jasmine/exports-only/test.spec.ts deleted file mode 100644 index cbe0a3d431..0000000000 --- a/tests-jasmine/exports-only/test.spec.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { TestBed } from '@angular/core/testing'; -import { MockBuilder, MockRender } from 'ng-mocks'; - -import { InternalComponent } from './fixtures.components'; -import { TargetModule } from './fixtures.modules'; - -describe('ExportsOnly:real', () => { - beforeEach(async done => { - await TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents(); - done(); - }); - - it('should render', () => { - const fixture = MockRender(InternalComponent); - expect(fixture).toBeDefined(); - expect(fixture.debugElement.nativeElement.innerHTML).toContain('internal'); - }); -}); - -describe('ExportsOnly:mock1', () => { - beforeEach(async done => { - await MockBuilder().mock(TargetModule); - done(); - }); - - // The expectation is to see that InternalModule was exported and it can be accessed from the test. - it('should render', () => { - const fixture = MockRender(InternalComponent); - expect(fixture).toBeDefined(); - const content = fixture.debugElement.nativeElement.innerHTML; - expect(content).toEqual(''); - }); -}); - -describe('ExportsOnly:mock2', () => { - beforeEach(async done => { - await MockBuilder().mock(TargetModule).mock(InternalComponent); - done(); - }); - - // The expectation is to see that InternalModule was exported and it can be accessed from the test. - it('should render', () => { - const fixture = MockRender(InternalComponent); - expect(fixture).toBeDefined(); - const content = fixture.debugElement.nativeElement.innerHTML; - expect(content).toEqual(''); - }); -}); - -describe('ExportsOnly:mock3', () => { - beforeEach(async done => { - await MockBuilder().keep(TargetModule); - done(); - }); - - // The expectation is to see that InternalModule was exported and it can be accessed from the test. - it('should render', () => { - const fixture = MockRender(InternalComponent); - expect(fixture).toBeDefined(); - expect(fixture.debugElement.nativeElement.innerHTML).toContain('internal'); - }); -}); diff --git a/tests-jasmine/injected-ng-templates/injected-ng-templates.spec.ts b/tests-jasmine/injected-ng-templates/injected-ng-templates.spec.ts deleted file mode 100644 index 946bf4e286..0000000000 --- a/tests-jasmine/injected-ng-templates/injected-ng-templates.spec.ts +++ /dev/null @@ -1,82 +0,0 @@ -import { DebugElement } from '@angular/core'; -import { TestBed } from '@angular/core/testing'; -import { MockComponent, MockRender, ngMocks } from 'ng-mocks'; - -import { CustomInjectionComponent } from './custom-injection.component'; - -describe('injected-ng-templates:real', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [CustomInjectionComponent], - }); - }); - - it('renders injected template properly', () => { - // should iterate against 3 string. - const fixture = MockRender( - ` - -
{{value}}
-
-
`, - { - values: ['string1', 'string2', 'string3'], - } - ); - - // every value should be rendered correctly. - expect(fixture.nativeElement.innerHTML).toContain('
string1
'); - expect(fixture.nativeElement.innerHTML).toContain('
string2
'); - expect(fixture.nativeElement.innerHTML).toContain('
string3
'); - }); -}); - -describe('structural-directive-as-ng-for:mock', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [MockComponent(CustomInjectionComponent)], - }); - }); - - it('renders mocked component with injected template properly', () => { - // do not remove, it checks casts from MockDebugElement to DebugElement. - let block: undefined | DebugElement; - - // should iterate against 3 string. - const fixture = MockRender( - ` - -
{{outside}} {{value}} {{custom}}
-
-
ng-content
-
`, - { - outside: 'string0', - values: ['string1', 'string2', 'string3'], - } - ); - - // By default @ContentChild('block') shouldn't be rendered at all. - block = ngMocks.find(fixture.debugElement, '[data-key="block"]', undefined); - expect(block).toBeUndefined(); - - const mockedComponent = ngMocks.find(fixture.debugElement, MockComponent(CustomInjectionComponent)) - .componentInstance; - - // Now we want to render @ContentChild('block') with some context. - mockedComponent.__render('block', 'string1', { - test: 'test1', - }); - fixture.detectChanges(); - block = ngMocks.find(fixture.debugElement, '[data-key="block"]'); - expect(block.nativeElement.innerHTML).toContain('
string0 string1 test1
'); - - // Now we want to render @ContentChild('block') with another context. - mockedComponent.__render('block', 'string2', { - test: 'test2', - }); - fixture.detectChanges(); - block = ngMocks.find(fixture.debugElement, '[data-key="block"]'); - expect(block.nativeElement.innerHTML).toContain('
string0 string2 test2
'); - }); -}); diff --git a/tests-jasmine/internal-only-nested/test.spec.ts b/tests-jasmine/internal-only-nested/test.spec.ts deleted file mode 100644 index 304eecd838..0000000000 --- a/tests-jasmine/internal-only-nested/test.spec.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { TestBed } from '@angular/core/testing'; -import { MockBuilder, MockRender } from 'ng-mocks'; - -import { InternalComponent } from './fixtures.components'; -import { TargetModule } from './fixtures.modules'; - -describe('InternalOnlyNested:real', () => { - beforeEach(async done => { - await TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents(); - done(); - }); - - it('should render', () => { - expect(() => { - MockRender(InternalComponent); - TestBed.get(InternalComponent); // Thanks Ivy True, it doesn't throw an error and we have to use injector. - }).toThrowError(); - }); -}); - -describe('InternalOnlyNested:mock', () => { - beforeEach(async done => { - await MockBuilder().mock(TargetModule).mock(InternalComponent, { export: true }); - done(); - }); - - // The expectation is to see that InternalComponent was exported to the level of the TestingModule - // and can be accessed in the test even it was deeply nested. - it('should render', () => { - const fixture = MockRender(InternalComponent); - expect(fixture).toBeDefined(); - const content = fixture.debugElement.nativeElement.innerHTML; - expect(content).toEqual(''); - }); -}); diff --git a/tests-jasmine/internal-only/test.spec.ts b/tests-jasmine/internal-only/test.spec.ts deleted file mode 100644 index 42d5e9332d..0000000000 --- a/tests-jasmine/internal-only/test.spec.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { TestBed } from '@angular/core/testing'; -import { MockBuilder, MockRender } from 'ng-mocks'; - -import { InternalComponent } from './fixtures.components'; -import { TargetModule } from './fixtures.modules'; - -describe('InternalOnly:real', () => { - beforeEach(async done => { - await TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents(); - done(); - }); - - it('should render', () => { - expect(() => { - MockRender(InternalComponent); - TestBed.get(InternalComponent); // Thanks Ivy True, it doesn't throw an error and we have to use injector. - }).toThrowError(); - }); -}); - -describe('InternalOnly:mock', () => { - beforeEach(async done => { - await MockBuilder().mock(TargetModule).mock(InternalComponent, { export: true }); - done(); - }); - - // The expectation is to see that InternalComponent was exported and can be accessed from the test. - it('should render', () => { - const fixture = MockRender(InternalComponent); - expect(fixture).toBeDefined(); - const content = fixture.debugElement.nativeElement.innerHTML; - expect(content).toEqual(''); - }); -}); diff --git a/tests-jasmine/normal-usage-after-mock-builder/test.spec.ts b/tests-jasmine/normal-usage-after-mock-builder/test.spec.ts deleted file mode 100644 index 5603426b8a..0000000000 --- a/tests-jasmine/normal-usage-after-mock-builder/test.spec.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { TestBed } from '@angular/core/testing'; -import { MockBuilder, MockRender } from 'ng-mocks'; - -import { FakeComponent, RealComponent, TargetComponent } from './fixtures.components'; -import { TargetModule } from './fixtures.modules'; - -describe('normal-usage-after-mock-builder:real1', () => { - beforeEach(() => - TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents() - ); - - it('renders real component because we did not use MockBuilder.replace yet', () => { - const fixture = MockRender(TargetComponent); - expect(fixture.debugElement.nativeElement.innerHTML).toEqual('real1'); - }); -}); - -describe('normal-usage-after-mock-builder:mock', () => { - beforeEach(() => - TestBed.configureTestingModule( - MockBuilder().keep(TargetModule).replace(RealComponent, FakeComponent, { dependency: true }).build() - ).compileComponents() - ); - - it('renders fake component because we used MockBuilder.replace', () => { - const fixture = MockRender(TargetComponent); - expect(fixture.debugElement.nativeElement.innerHTML).toEqual('fake1'); - }); -}); - -describe('normal-usage-after-mock-builder:real2', () => { - beforeEach(() => - TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents() - ); - - it('has to render real component after MockBuilder.replace', () => { - const fixture = MockRender(TargetComponent); - expect(fixture.debugElement.nativeElement.innerHTML).toEqual('real1'); - }); -}); diff --git a/tests-jasmine/on-push/on-push.spec.ts b/tests-jasmine/on-push/on-push.spec.ts deleted file mode 100644 index 0bdd8b96fe..0000000000 --- a/tests-jasmine/on-push/on-push.spec.ts +++ /dev/null @@ -1,95 +0,0 @@ -import { ChangeDetectionStrategy, Component, Input } from '@angular/core'; -import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { MockedDebugElement, MockRender, ngMocks } from 'ng-mocks'; - -@Component({ - changeDetection: ChangeDetectionStrategy.OnPush, - selector: 'item-list', - template: '{{items.length}}', -}) -export class ItemListComponent { - @Input() items: string[]; -} - -@Component({ - selector: 'item-list-wrapper', - template: '', -}) -export class ItemListWrapperComponent { - @Input() items: string[]; -} - -describe('ChangeDetectionStrategy.OnPush:real', () => { - let wrapper: ComponentFixture; - let componentDebugElement: MockedDebugElement; - - beforeEach(() => { - // const wrapperType = WrapComponent(ItemListComponent); - TestBed.configureTestingModule({ - declarations: [ - ItemListComponent, - ItemListWrapperComponent, - // wrapperType, - ], - }); - - wrapper = TestBed.createComponent(ItemListWrapperComponent); - wrapper.componentInstance.items = []; - wrapper.detectChanges(); - - componentDebugElement = ngMocks.find(wrapper.debugElement, ItemListComponent); - }); - - it('should show 0 if no items', () => { - expect(componentDebugElement.nativeElement.innerHTML).toEqual('0'); - }); - - it('should show 0 if items pushed to array but not changed reference', () => { - wrapper.componentInstance.items.push('demo'); - wrapper.detectChanges(); - - expect(componentDebugElement.nativeElement.innerHTML).toEqual('0'); - }); - - it('should show 1 if items array changed reference', () => { - wrapper.componentInstance.items = ['demo']; - wrapper.detectChanges(); - - expect(componentDebugElement.nativeElement.innerHTML).toEqual('1'); - }); -}); - -describe('ChangeDetectionStrategy.OnPush:mock', () => { - let fixture: ComponentFixture; - let component: ItemListComponent; - - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [ItemListComponent], - }); - - fixture = MockRender(ItemListComponent, { - items: [], - }); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should show 0 if no items', () => { - expect(fixture.nativeElement.innerHTML).toContain('>0<'); - }); - - it('should show 0 if items pushed to array but not changed reference', () => { - component.items.push('demo'); - fixture.detectChanges(); - - expect(fixture.nativeElement.innerHTML).toContain('>0<'); - }); - - it('should show 1 if items array changed reference', () => { - component.items = ['demo']; - fixture.detectChanges(); - - expect(fixture.nativeElement.innerHTML).toContain('>1<'); - }); -}); diff --git a/tests-jasmine/spies/test.spec.ts b/tests-jasmine/spies/test.spec.ts deleted file mode 100644 index 134521902b..0000000000 --- a/tests-jasmine/spies/test.spec.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { inject, TestBed } from '@angular/core/testing'; -import { MockModule, MockRender, ngMocks } from 'ng-mocks'; - -import { TargetComponent } from './fixtures.components'; -import { TargetModule } from './fixtures.modules'; -import { TargetService } from './fixtures.providers'; - -describe('spies:real', () => { - beforeEach(() => - TestBed.configureTestingModule({ - declarations: [TargetComponent], - imports: [TargetModule], - }).compileComponents() - ); - - it('should render', () => { - const fixture = MockRender(TargetComponent); - const component = ngMocks.find(fixture.debugElement, TargetComponent).componentInstance; - expect(component).toBeDefined(); - expect(component.echo()).toEqual('TargetComponent'); - }); -}); - -describe('spies:manual-mock', () => { - beforeEach(() => { - const spy = jasmine.createSpyObj('TargetService', ['echo']); - spy.echo.and.returnValue('fake'); - - return TestBed.configureTestingModule({ - declarations: [TargetComponent], - imports: [MockModule(TargetModule)], - providers: [ - { - provide: TargetService, - useValue: spy, - }, - ], - }).compileComponents(); - }); - - it('should get manually mocked service', inject([TargetService], (targetService: TargetService) => { - const fixture = MockRender(TargetComponent); - const component = ngMocks.find(fixture.debugElement, TargetComponent).componentInstance; - expect(component).toBeDefined(); - expect(targetService.echo).toHaveBeenCalledTimes(1); - expect(targetService.echo).toHaveBeenCalledWith('constructor'); - expect(component.echo()).toEqual('fake'); - expect(targetService.echo).toHaveBeenCalledTimes(2); - })); -}); - -describe('spies:auto-mock', () => { - beforeEach(() => - TestBed.configureTestingModule({ - declarations: [TargetComponent], - imports: [MockModule(TargetModule)], - }).compileComponents() - ); - - it('should get already mocked service', inject([TargetService], (targetService: TargetService) => { - const fixture = MockRender(TargetComponent); - const component = ngMocks.find(fixture.debugElement, TargetComponent).componentInstance; - expect(component).toBeDefined(); - expect(targetService.echo).toHaveBeenCalledTimes(1); - expect(targetService.echo).toHaveBeenCalledWith('constructor'); - ngMocks.stub(targetService, 'echo').and.returnValue('faked'); - expect(component.echo()).toEqual('faked'); - expect(targetService.echo).toHaveBeenCalledTimes(2); - })); -}); diff --git a/tests-jasmine/structural-directives/structural-directives.spec.ts b/tests-jasmine/structural-directives/structural-directives.spec.ts deleted file mode 100644 index 19e61e1aec..0000000000 --- a/tests-jasmine/structural-directives/structural-directives.spec.ts +++ /dev/null @@ -1,287 +0,0 @@ -import { TestBed } from '@angular/core/testing'; -import { MockDirective, MockedDirective, MockRender, ngMocks } from 'ng-mocks'; - -import { CustomNgForWithOfDirective } from './custom-ng-for-with-of.directive'; -import { CustomNgForWithoutOfDirective } from './custom-ng-for-without-of.directive'; -import { CustomNgIfDirective } from './custom-ng-if.directive'; - -describe('structural-directive-as-ng-for:real', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [CustomNgForWithOfDirective, CustomNgForWithoutOfDirective, CustomNgIfDirective], - }); - }); - - it('renders customNgForWithOf properly', () => { - // should iterate against 3 string. - const fixture = MockRender( - ` -
w/ {{v}}{{i}}{{f ? 1 : 0}}{{l ? 1 : 0}}
- `, - { - values: ['string1', 'string2', 'string3'], - } - ); - - // every value should be rendered correctly. - expect(fixture.nativeElement.innerHTML).toContain('
w/ string1010
'); - expect(fixture.nativeElement.innerHTML).toContain('
w/ string2100
'); - expect(fixture.nativeElement.innerHTML).toContain('
w/ string3201
'); - }); - - it('renders customNgForWithoutOf properly', () => { - // should iterate against 3 string. - const fixture = MockRender( - ` -
w/o {{v}}{{i}}{{f ? 1 : 0}}{{l ? 1 : 0}}
- `, - { - values: ['string1', 'string2', 'string3'], - } - ); - - // every value should be rendered correctly. - expect(fixture.nativeElement.innerHTML).toContain('
w/o string1010
'); - expect(fixture.nativeElement.innerHTML).toContain('
w/o string2100
'); - expect(fixture.nativeElement.innerHTML).toContain('
w/o string3201
'); - }); - - it('renders customNgIf properly', () => { - // should display just first part. - const fixture = MockRender( - ` -
should be shown
-
should be hidden
- `, - { - values: ['string1', 'string2', 'string3'], - } - ); - - // only first div should be rendered. - expect(fixture.nativeElement.innerHTML).toContain('
should be shown
'); - expect(fixture.nativeElement.innerHTML).not.toContain('
should be hidden
'); - }); -}); - -describe('structural-directive-as-ng-for:mock', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [ - MockDirective(CustomNgIfDirective), - MockDirective(CustomNgForWithOfDirective), - MockDirective(CustomNgForWithoutOfDirective), - ], - }); - }); - - it('mocks customNgIf properly', () => { - const fixture = MockRender( - ` -
- $implicit:{{ data | json }} - fromDirective:{{ randomParam | json }} -
- `, - { - values: ['string1', 'string2', 'string3'], - } - ); - - // we need to render mocked structural directives manually - ngMocks - .findInstances(fixture.debugElement, CustomNgIfDirective) - .forEach((item: MockedDirective) => - item.__render(undefined, { - fromDirective: undefined, - }) - ); - fixture.detectChanges(); - - // By default mocked structural directives are rendered with undefined variables. - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toContain(' $implicit: fromDirective: '); - - // Extracting mock. - const debugElement = ngMocks.find(fixture.debugElement, 'div'); - const directive = ngMocks.get(debugElement, MockDirective(CustomNgIfDirective)); - expect(directive).toBeTruthy(); - if (!directive) { - return; - } - - // Assert that mock got right variables. - expect(directive.setValue).toEqual(['string1', 'string2', 'string3']); - - // Now we want to assert how our html is rendered based on structural directive variables' definition. - directive.__render(true, { - fromDirective: false, - }); - fixture.detectChanges(); - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toContain(' $implicit:true fromDirective:false '); - - // And we want dynamically change variables for render. - directive.__render(false, { - fromDirective: true, - }); - fixture.detectChanges(); - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toContain(' $implicit:false fromDirective:true '); - }); - - it('mocks CustomNgForWithOfDirective properly', () => { - const fixture = MockRender( - ` -
- w/ {{v}}{{i}}{{f ? 1 : 0}}{{l ? 1 : 0}} -
- `, - { - values: ['string1', 'string2', 'string3'], - } - ); - - // we need to render mocked structural directives manually - ngMocks - .findInstances(fixture.debugElement, CustomNgForWithOfDirective) - .forEach((item: MockedDirective) => item.__render()); - fixture.detectChanges(); - - // By default mocked structural directives are rendered with undefined variables. - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toContain(' w/ 00 '); - - const debugElement = ngMocks.find(fixture.debugElement, 'div'); - - // Extracting mock. - const directive = ngMocks.get(debugElement, MockDirective(CustomNgForWithOfDirective)); - expect(directive).toBeTruthy(); - if (!directive) { - return; - } - - // Assert that mock got right variables. - expect(directive.setItems).toEqual(['string1', 'string2', 'string3']); - - // Now we want to assert how our html is rendered based on structural directive variables' definition. - directive.__render('MainValue', { - myFirst: true, - myIndex: 'MyIndex', - myLast: false, - }); - fixture.detectChanges(); - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toContain(' w/ MainValueMyIndex10 '); - - // And we want dynamically change variables for render. - directive.__render('MainValue2', { - myFirst: false, - myIndex: 'MyIndex2', - myLast: true, - }); - fixture.detectChanges(); - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toContain(' w/ MainValue2MyIndex201 '); - }); - - it('mocks customNgForWithoutOf properly', () => { - const fixture = MockRender( - ` -
- w/o {{v}}{{i}}{{f ? 1 : 0}}{{l ? 1 : 0}} -
- `, - { - values: ['string1', 'string2', 'string3'], - } - ); - - // we need to render mocked structural directives manually - ngMocks - .findInstances(fixture.debugElement, CustomNgForWithoutOfDirective) - .forEach((item: MockedDirective) => item.__render()); - fixture.detectChanges(); - - // By default mocked structural directives are rendered with undefined variables. - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toContain(' w/o 00 '); - - const debugElement = ngMocks.find(fixture.debugElement, 'div'); - - // Extracting mock. - const directive = ngMocks.get(debugElement, MockDirective(CustomNgForWithoutOfDirective)); - expect(directive).toBeTruthy(); - if (!directive) { - return; - } - - // Assert that mock got right variables. - expect(directive.setItems).toEqual(['string1', 'string2', 'string3']); - - // Now we want to assert how our html is rendered based on structural directive variables' definition. - directive.__render('MainValue', { - myFirst: true, - myIndex: 'MyIndex', - myLast: false, - }); - fixture.detectChanges(); - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toContain(' w/o MainValueMyIndex10 '); - - // And we want dynamically change variables for render. - directive.__render('MainValue2', { - myFirst: false, - myIndex: 'MyIndex2', - myLast: true, - }); - fixture.detectChanges(); - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toContain(' w/o MainValue2MyIndex201 '); - }); - - it('searches for related directive', () => { - let mockedDirective: MockedDirective | undefined; - - const fixture = MockRender( - ` -
- w/o {{v}}{{i}}{{f ? 1 : 0}}{{l ? 1 : 0}} -
-
- w/o {{v}}{{i}}{{f ? 1 : 0}}{{l ? 1 : 0}} -
- `, - { - values1: ['string1', 'string2', 'string3'], - values2: ['string4', 'string5', 'string6'], - } - ); - - // we need to render mocked structural directives manually - ngMocks - .findInstances(fixture.debugElement, CustomNgForWithOfDirective) - .forEach((item: MockedDirective) => item.__render()); - ngMocks - .findInstances(fixture.debugElement, CustomNgForWithoutOfDirective) - .forEach((item: MockedDirective) => item.__render()); - fixture.detectChanges(); - - // Looking for first directive. - mockedDirective = ngMocks.get( - ngMocks.find(fixture.debugElement, '[data-type="node-1"]'), - MockDirective(CustomNgForWithoutOfDirective) - ); - expect(mockedDirective.setItems).toEqual(['string1', 'string2', 'string3']); - - // Looking for second directive. - mockedDirective = ngMocks.get( - ngMocks.find(fixture.debugElement, '[data-type="node-2"]'), - MockDirective(CustomNgForWithoutOfDirective) - ); - expect(mockedDirective.setItems).toEqual(['string4', 'string5', 'string6']); - }); -}); diff --git a/tests-jest/context-with-directives/custom-root.component.ts b/tests-jest/context-with-directives/custom-root.component.ts deleted file mode 100644 index d9abe87d15..0000000000 --- a/tests-jest/context-with-directives/custom-root.component.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { AfterContentInit, Component, ContentChildren, QueryList, TemplateRef } from '@angular/core'; - -import { CustomTypeDirective } from './custom-type.directive'; - -@Component({ - selector: 'custom-root', - template: ` -
- -
-
- -
-
- -
- `, -}) -export class CustomRootComponent implements AfterContentInit { - public context = ['0']; - public context1 = ['1']; - public template: TemplateRef; - public template1: TemplateRef; - public template2: TemplateRef; - @ContentChildren(CustomTypeDirective) templates: QueryList; - - ngAfterContentInit(): void { - this.templates.forEach((template: CustomTypeDirective) => { - switch (template.type) { - case 'template1': - this.template1 = template.template; - break; - case 'template2': - this.template2 = template.template; - break; - default: - this.template = template.template; - } - }); - } -} diff --git a/tests-jest/context-with-directives/custom-type.directive.ts b/tests-jest/context-with-directives/custom-type.directive.ts deleted file mode 100644 index aaeaed26e1..0000000000 --- a/tests-jest/context-with-directives/custom-type.directive.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Directive, Input, TemplateRef } from '@angular/core'; - -@Directive({ - selector: '[type]', -}) -export class CustomTypeDirective { - public template: TemplateRef; - @Input('type') type: string; - - constructor(template: TemplateRef) { - this.template = template; - } -} diff --git a/tests-jest/control-value-accessor-form-control/fixtures.ts b/tests-jest/control-value-accessor-form-control/fixtures.ts deleted file mode 100644 index 5bf4d5500f..0000000000 --- a/tests-jest/control-value-accessor-form-control/fixtures.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { CommonModule } from '@angular/common'; -import { Component, forwardRef, NgModule } from '@angular/core'; -import { ControlValueAccessor, FormControl, NG_VALUE_ACCESSOR, ReactiveFormsModule } from '@angular/forms'; - -@Component({ - selector: 'target', - template: '', -}) -export class TargetComponent { - public readonly control = new FormControl(); -} - -@Component({ - providers: [ - { - multi: true, - provide: NG_VALUE_ACCESSOR, - useExisting: forwardRef(() => ControlComponent), - }, - ], - selector: 'control', - template: '', -}) -export class ControlComponent implements ControlValueAccessor { - public isDisabled = false; - public value: any; - public change: any = () => undefined; - - changeTouch(): void { - this.touch(); - } - - changeValue(obj: any): void { - this.change(obj); - } - - registerOnChange(fn: any): void { - this.change = fn; - } - - registerOnTouched(fn: any): void { - this.touch = fn; - } - - setDisabledState(isDisabled: boolean): void { - this.isDisabled = isDisabled; - } - - public touch: any = () => undefined; - - writeValue(obj: any): void { - this.value = obj; - } -} - -@NgModule({ - declarations: [TargetComponent, ControlComponent], - exports: [TargetComponent], - imports: [CommonModule, ReactiveFormsModule], -}) -export class TargetModule {} diff --git a/tests-jest/control-value-accessor-ng-model/fixtures.ts b/tests-jest/control-value-accessor-ng-model/fixtures.ts deleted file mode 100644 index c423dfcbe3..0000000000 --- a/tests-jest/control-value-accessor-ng-model/fixtures.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { CommonModule } from '@angular/common'; -import { Component, forwardRef, NgModule } from '@angular/core'; -import { ControlValueAccessor, FormsModule, NG_VALUE_ACCESSOR } from '@angular/forms'; - -@Component({ - selector: 'target', - template: '', -}) -export class TargetComponent { - public disabled = false; - public realValue: null | string = null; - - public get value(): null | string { - return this.realValue; - } - - public set value(value: null | string) { - this.realValue = value; - } -} - -@Component({ - providers: [ - { - multi: true, - provide: NG_VALUE_ACCESSOR, - useExisting: forwardRef(() => ControlComponent), - }, - ], - selector: 'control', - template: '', -}) -export class ControlComponent implements ControlValueAccessor { - public isDisabled = false; - public value: any; - public change: any = () => undefined; - - changeTouch(): void { - this.touch(); - } - - changeValue(obj: any): void { - this.change(obj); - } - - registerOnChange(fn: any): void { - this.change = fn; - } - - registerOnTouched(fn: any): void { - this.touch = fn; - } - - setDisabledState(isDisabled: boolean): void { - this.isDisabled = isDisabled; - } - - public touch: any = () => undefined; - - writeValue(obj: any): void { - this.value = obj; - } -} - -@NgModule({ - declarations: [TargetComponent, ControlComponent], - exports: [TargetComponent], - imports: [CommonModule, FormsModule], -}) -export class TargetModule {} diff --git a/tests-jest/control-value-accessor-ng-model/test.spec.ts b/tests-jest/control-value-accessor-ng-model/test.spec.ts deleted file mode 100644 index 0817a7e3b9..0000000000 --- a/tests-jest/control-value-accessor-ng-model/test.spec.ts +++ /dev/null @@ -1,129 +0,0 @@ -import { FormsModule, NgModel } from '@angular/forms'; -import { MockBuilder, MockComponent, MockHelper, MockRender } from 'ng-mocks'; - -import { ControlComponent, TargetComponent, TargetModule } from './fixtures'; - -// a real case to check possible behavior. -describe('control-value-accessor-ng-model:real', () => { - beforeEach(() => MockBuilder(TargetComponent).keep(TargetModule)); - - it('respects our ngModel', async () => { - const fixture = MockRender(TargetComponent, {}, false); - const mockElement = MockHelper.findOrFail(fixture.debugElement, ControlComponent); - const mock = mockElement.componentInstance; - spyOn(mock, 'writeValue').and.callThrough(); - spyOn(mock, 'setDisabledState').and.callThrough(); - const ngModel = MockHelper.getDirectiveOrFail(mockElement, NgModel); - fixture.detectChanges(); - await fixture.whenStable(); - - expect(mock.writeValue).toHaveBeenCalledWith(null); - expect(mock.setDisabledState).not.toHaveBeenCalled(); - expect(ngModel.touched).toBeFalsy(); - - // checking via original component - fixture.point.componentInstance.value = 'test1'; - fixture.detectChanges(); - await fixture.whenStable(); - expect(mock.writeValue).toHaveBeenCalledWith('test1'); - expect(ngModel.touched).toBeFalsy(); - - fixture.point.componentInstance.value = 'test2'; - fixture.detectChanges(); - await fixture.whenStable(); - expect(mock.writeValue).toHaveBeenCalledWith('test2'); - expect(ngModel.touched).toBeFalsy(); - - // checking that touch works - mock.changeTouch(); - expect(ngModel.touched).toBeTruthy(); - - // checking that reset works - ngModel.control.markAsUntouched(); - expect(ngModel.touched).toBeFalsy(); - - // checking that disabled works - fixture.point.componentInstance.disabled = true; - fixture.detectChanges(); - await fixture.whenStable(); - expect(mock.setDisabledState).toHaveBeenCalledWith(true); - fixture.point.componentInstance.disabled = false; - fixture.detectChanges(); - await fixture.whenStable(); - expect(mock.setDisabledState).toHaveBeenCalledWith(false); - - // changeValue doesn't trigger anything else but the callback. Therefore it doesn't render new value. - // It only updates the original control's value. - mock.changeValue('test3'); - expect(mock.writeValue).not.toHaveBeenCalledWith('test3'); - expect(ngModel.touched).toBeFalsy(); - expect(fixture.point.componentInstance.value).toBe('test3'); - }); -}); - -// a way that ensures that a mocked component behaves the same way as real one. -describe('control-value-accessor-ng-model:mock', () => { - beforeEach(() => MockBuilder(TargetComponent, TargetModule).keep(FormsModule)); - - it('respects our ngModel', async () => { - const fixture = MockRender(TargetComponent, {}, false); - const mockElement = MockHelper.findOrFail(fixture.debugElement, MockComponent(ControlComponent)); - const mock = mockElement.componentInstance; - spyOn(mock, 'writeValue').and.callThrough(); - spyOn(mock, 'setDisabledState').and.callThrough(); - spyOn(mock, 'registerOnChange').and.callThrough(); - spyOn(mock, 'registerOnTouched').and.callThrough(); - const ngModel = MockHelper.getDirectiveOrFail(mockElement, NgModel); - fixture.detectChanges(); - await fixture.whenStable(); - - expect(mock.writeValue).toHaveBeenCalledWith(null); - expect(mock.setDisabledState).not.toHaveBeenCalled(); - expect(ngModel.touched).toBeFalsy(); - - // checking via original component - fixture.point.componentInstance.value = 'test1'; - fixture.detectChanges(); - await fixture.whenStable(); - expect(mock.writeValue).toHaveBeenCalledWith('test1'); - expect(ngModel.touched).toBeFalsy(); - - fixture.point.componentInstance.value = 'test2'; - fixture.detectChanges(); - await fixture.whenStable(); - expect(mock.writeValue).toHaveBeenCalledWith('test2'); - expect(ngModel.touched).toBeFalsy(); - - // checking that touch works - mock.__simulateTouch(); - expect(ngModel.touched).toBeTruthy(); - ngModel.control.markAsUntouched(); - expect(ngModel.touched).toBeFalsy(); - // a way through a spy - MockHelper.mockService(mock, 'registerOnTouched').calls.first().args[0](); - expect(ngModel.touched).toBeTruthy(); - ngModel.control.markAsUntouched(); - - // checking that disabled works - fixture.point.componentInstance.disabled = true; - fixture.detectChanges(); - await fixture.whenStable(); - expect(mock.setDisabledState).toHaveBeenCalledWith(true); - fixture.point.componentInstance.disabled = false; - fixture.detectChanges(); - await fixture.whenStable(); - expect(mock.setDisabledState).toHaveBeenCalledWith(false); - - // changeValue doesn't trigger anything else but the callback. Therefore it doesn't render new value. - // It only updates the original control's value. - mock.__simulateChange('test3'); - expect(mock.writeValue).not.toHaveBeenCalledWith('test3'); - expect(ngModel.touched).toBeFalsy(); - expect(fixture.point.componentInstance.value).toBe('test3'); - // a way through a spy - MockHelper.mockService(mock, 'registerOnChange').calls.first().args[0]('test4'); - expect(mock.writeValue).not.toHaveBeenCalledWith('test4'); - expect(ngModel.touched).toBeFalsy(); - expect(ngModel.value).toBe('test4'); - }); -}); diff --git a/tests-jest/exports-only/fixtures.components.ts b/tests-jest/exports-only/fixtures.components.ts deleted file mode 100644 index 9fd4b87b11..0000000000 --- a/tests-jest/exports-only/fixtures.components.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'internal-component', - template: 'internal', -}) -export class InternalComponent {} diff --git a/tests-jest/exports-only/fixtures.modules.ts b/tests-jest/exports-only/fixtures.modules.ts deleted file mode 100644 index 95e2906ecc..0000000000 --- a/tests-jest/exports-only/fixtures.modules.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { CommonModule } from '@angular/common'; -import { NgModule } from '@angular/core'; - -import { InternalComponent } from './fixtures.components'; - -@NgModule({ - declarations: [InternalComponent], - exports: [InternalComponent], - imports: [CommonModule], -}) -export class InternalModule {} - -@NgModule({ - exports: [InternalModule], -}) -export class TargetModule {} diff --git a/tests-jest/get-inputs-and-outputs/fixtures.ts b/tests-jest/get-inputs-and-outputs/fixtures.ts deleted file mode 100644 index 88000035aa..0000000000 --- a/tests-jest/get-inputs-and-outputs/fixtures.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { Component, Directive, EventEmitter, Input, NgModule, Output } from '@angular/core'; - -@Component({ - selector: 'target', - template: '{{ input }}', -}) -export class TargetComponent { - @Input('input1') input: string; - @Output('output1') output = new EventEmitter(); -} - -@Directive({ - selector: 'target', -}) -export class Target2Directive { - @Input('input2') input: string; - @Input('inputUnused') input2: undefined; - @Output('output2') output = new EventEmitter(); -} - -@Directive({ - selector: 'target', -}) -export class Target3Directive { - @Input('input3') input: string; - @Output('output3') output = new EventEmitter(); -} - -@NgModule({ - declarations: [TargetComponent, Target2Directive, Target3Directive], - exports: [TargetComponent, Target2Directive, Target3Directive], -}) -export class TargetModule {} diff --git a/tests-jest/get-inputs-and-outputs/test.spec.ts b/tests-jest/get-inputs-and-outputs/test.spec.ts deleted file mode 100644 index ab30fa8ca1..0000000000 --- a/tests-jest/get-inputs-and-outputs/test.spec.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { TestBed } from '@angular/core/testing'; -import { MockRender, ngMocks } from 'ng-mocks'; - -import { Target2Directive, Target3Directive, TargetComponent, TargetModule } from './fixtures'; - -describe('get-inputs-and-outputs', () => { - beforeEach(() => - TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents() - ); - - it('finds them correctly', () => { - const params = { - input1: '1', - input2: '2', - output1: jest.fn().mockName('output1'), - output2: jest.fn().mockName('output2'), - output3: jest.fn().mockName('output3'), - }; - const fixture = MockRender( - ``, - params - ); - - const componentElement = fixture.point; - const component = fixture.point.componentInstance; - const directive2 = ngMocks.get(componentElement, Target2Directive); - const directive3 = ngMocks.get(componentElement, Target3Directive); - - expect(component.input).toEqual('1'); - params.output1.mockReset(); - component.output.emit(); - expect(params.output1).toHaveBeenCalled(); - - expect(directive2.input).toEqual('2'); - expect(directive2.input2).toEqual(undefined); - params.output2.mockReset(); - directive2.output.emit(); - expect(params.output2).toHaveBeenCalled(); - - expect(directive3.input).toEqual('3'); - params.output3.mockReset(); - directive3.output.emit(); - expect(params.output3).toHaveBeenCalled(); - - // a really simple wait that allows us to skip pain of knowing directives. - expect(ngMocks.input(componentElement, 'input1')).toEqual('1'); - expect(ngMocks.input(componentElement, 'input2')).toEqual('2'); - expect(ngMocks.input(componentElement, 'inputUnused')).toEqual(undefined); - expect(() => ngMocks.input(componentElement, 'inputUndefined')).toThrowError( - 'Cannot find inputUndefined input via ngMocks.input' - ); - expect(ngMocks.input(componentElement, 'input3')).toEqual('3'); - params.output1.mockReset(); - ngMocks.output(componentElement, 'output1').emit(); - expect(params.output1).toHaveBeenCalled(); - params.output2.mockReset(); - ngMocks.output(componentElement, 'output2').emit(); - expect(params.output2).toHaveBeenCalled(); - params.output3.mockReset(); - ngMocks.output(componentElement, 'output3').emit(); - expect(params.output3).toHaveBeenCalled(); - expect(() => ngMocks.output(componentElement, 'outputUndefined')).toThrowError( - 'Cannot find outputUndefined input via ngMocks.output' - ); - }); -}); diff --git a/tests-jest/index.ts b/tests-jest/index.ts deleted file mode 100644 index c730517a9e..0000000000 --- a/tests-jest/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -// Dirty 'any' hack to avoid issues to run tests on angular 8 when the 'static' key is required. -export const staticFalse: any = { - static: false, -}; -export const staticTrue: any = { - static: true, -}; diff --git a/tests-jest/injected-ng-templates/custom-injection.component.ts b/tests-jest/injected-ng-templates/custom-injection.component.ts deleted file mode 100644 index 453f894806..0000000000 --- a/tests-jest/injected-ng-templates/custom-injection.component.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Component, ContentChild, Input, TemplateRef, ViewChild, ViewContainerRef } from '@angular/core'; - -import { staticFalse } from '..'; - -@Component({ - selector: 'custom-injection', - template: ` -
- -
-
- `, -}) -export class CustomInjectionComponent { - @ContentChild('block', { ...staticFalse }) injectedBlock: TemplateRef; - @Input() items?: T[]; - @ViewChild('child', { ...staticFalse }) ownChild: ViewContainerRef; -} diff --git a/tests-jest/internal-only-nested/fixtures.components.ts b/tests-jest/internal-only-nested/fixtures.components.ts deleted file mode 100644 index 9fd4b87b11..0000000000 --- a/tests-jest/internal-only-nested/fixtures.components.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'internal-component', - template: 'internal', -}) -export class InternalComponent {} diff --git a/tests-jest/internal-only-nested/fixtures.modules.ts b/tests-jest/internal-only-nested/fixtures.modules.ts deleted file mode 100644 index 643509a790..0000000000 --- a/tests-jest/internal-only-nested/fixtures.modules.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { CommonModule } from '@angular/common'; -import { NgModule } from '@angular/core'; - -import { InternalComponent } from './fixtures.components'; - -@NgModule({ - declarations: [InternalComponent], - imports: [CommonModule], -}) -export class Nested1Module {} - -@NgModule({ - imports: [Nested1Module], -}) -export class Nested2Module {} - -@NgModule({ - imports: [Nested1Module], -}) -export class Nested3Module {} - -@NgModule({ - imports: [Nested2Module, Nested3Module], -}) -export class TargetModule {} diff --git a/tests-jest/internal-only/fixtures.components.ts b/tests-jest/internal-only/fixtures.components.ts deleted file mode 100644 index 9fd4b87b11..0000000000 --- a/tests-jest/internal-only/fixtures.components.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'internal-component', - template: 'internal', -}) -export class InternalComponent {} diff --git a/tests-jest/internal-only/fixtures.modules.ts b/tests-jest/internal-only/fixtures.modules.ts deleted file mode 100644 index b9d4de82e8..0000000000 --- a/tests-jest/internal-only/fixtures.modules.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { CommonModule } from '@angular/common'; -import { NgModule } from '@angular/core'; - -import { InternalComponent } from './fixtures.components'; - -@NgModule({ - declarations: [InternalComponent], - imports: [CommonModule], -}) -export class TargetModule {} diff --git a/tests-jest/internal-vs-external/fixtures.components.ts b/tests-jest/internal-vs-external/fixtures.components.ts deleted file mode 100644 index bc86a0d881..0000000000 --- a/tests-jest/internal-vs-external/fixtures.components.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'internal-component', - template: 'internal', -}) -export class InternalComponent {} - -@Component({ - selector: 'external-component', - template: 'external ', -}) -export class ExternalComponent {} diff --git a/tests-jest/internal-vs-external/fixtures.modules.ts b/tests-jest/internal-vs-external/fixtures.modules.ts deleted file mode 100644 index f7c1d56d56..0000000000 --- a/tests-jest/internal-vs-external/fixtures.modules.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { CommonModule } from '@angular/common'; -import { NgModule } from '@angular/core'; - -import { ExternalComponent, InternalComponent } from './fixtures.components'; - -@NgModule({ - declarations: [InternalComponent, ExternalComponent], - exports: [ExternalComponent], - imports: [CommonModule], -}) -export class TargetModule {} diff --git a/tests-jest/internal-vs-external/test.spec.ts b/tests-jest/internal-vs-external/test.spec.ts deleted file mode 100644 index fc2a47279a..0000000000 --- a/tests-jest/internal-vs-external/test.spec.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { TestBed } from '@angular/core/testing'; -import { MockBuilder, MockModule, MockRender } from 'ng-mocks'; - -import { ExternalComponent, InternalComponent } from './fixtures.components'; -import { TargetModule } from './fixtures.modules'; - -describe('InternalVsExternal:real', () => { - beforeEach(async done => { - await TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents(); - done(); - }); - - it('should render', () => { - const fixture = MockRender(ExternalComponent); - expect(fixture).toBeDefined(); - const content = fixture.debugElement.nativeElement.innerHTML; - expect(content).toContain('external internal'); - - expect(() => { - MockRender(InternalComponent); - TestBed.get(InternalComponent); // Thanks Ivy True, it doesn't throw an error and we have to use injector. - }).toThrowError(); - }); -}); - -describe('InternalVsExternal:mock', () => { - beforeEach(async done => { - await MockBuilder().mock(TargetModule); - done(); - }); - - // The expectation is to see that ExternalComponent was exported and InternalComponent wasn't. - it('should render', () => { - const fixture = MockRender(ExternalComponent); - expect(fixture).toBeDefined(); - const content = fixture.debugElement.nativeElement.innerHTML; - expect(content).toEqual(''); - - expect(() => { - MockRender(InternalComponent); - TestBed.get(InternalComponent); // Thanks Ivy True, it doesn't throw an error and we have to use injector. - }).toThrowError(); - }); -}); - -describe('InternalVsExternal:legacy', () => { - beforeEach(async done => { - await TestBed.configureTestingModule({ - imports: [MockModule(TargetModule)], - }).compileComponents(); - done(); - }); - - it('should render', () => { - const fixture = MockRender(ExternalComponent); - expect(fixture).toBeDefined(); - const content = fixture.debugElement.nativeElement.innerHTML; - expect(content).toEqual(''); - - // the code below will fail because the MockModule outside of the MockBuilder exports everything. - // try { - // MockRender(InternalComponent); - // fail('should fail on the internal component'); - // } catch (e) { - // expect(e).toEqual(jasmine.objectContaining({ngSyntaxError: true})); - // } - }); -}); diff --git a/tests-jest/mock-builder-by-directive/fixtures.components.ts b/tests-jest/mock-builder-by-directive/fixtures.components.ts deleted file mode 100644 index 9fd4b87b11..0000000000 --- a/tests-jest/mock-builder-by-directive/fixtures.components.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'internal-component', - template: 'internal', -}) -export class InternalComponent {} diff --git a/tests-jest/mock-builder-by-directive/fixtures.modules.ts b/tests-jest/mock-builder-by-directive/fixtures.modules.ts deleted file mode 100644 index 430a37d058..0000000000 --- a/tests-jest/mock-builder-by-directive/fixtures.modules.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { NgModule } from '@angular/core'; - -import { InternalComponent } from './fixtures.components'; - -@NgModule({ - declarations: [InternalComponent], - exports: [InternalComponent], -}) -export class TargetModule {} diff --git a/tests-jest/mock-builder-by-directive/test.spec.ts b/tests-jest/mock-builder-by-directive/test.spec.ts deleted file mode 100644 index 524076ff9c..0000000000 --- a/tests-jest/mock-builder-by-directive/test.spec.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { TestBed } from '@angular/core/testing'; -import { MockBuilder, MockComponent, MockRender, ngMocks } from 'ng-mocks'; - -import { InternalComponent } from './fixtures.components'; -import { TargetModule } from './fixtures.modules'; - -describe('MockBuilderByDirective:real', () => { - beforeEach(async done => { - await TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents(); - done(); - }); - - it('should render', () => { - const fixture = MockRender(InternalComponent); - const element = ngMocks.find(fixture.debugElement, InternalComponent, undefined); - expect(element).toBeDefined(); - }); -}); - -describe('MockBuilderByDirective:mock', () => { - beforeEach(async done => { - await MockBuilder().mock(TargetModule); - done(); - }); - - it('should find mock', () => { - const fixture = MockRender(InternalComponent); - const element = ngMocks.find(fixture.debugElement, MockComponent(InternalComponent), undefined); - expect(element).toBeDefined(); - }); - - it('should find original', () => { - const fixture = MockRender(InternalComponent); - const element = ngMocks.find(fixture.debugElement, InternalComponent, undefined); - expect(element).toBeDefined(); - }); -}); diff --git a/tests-jest/mock-builder-keeps-application-module/fixtures.components.ts b/tests-jest/mock-builder-keeps-application-module/fixtures.components.ts deleted file mode 100644 index d23ac6421a..0000000000 --- a/tests-jest/mock-builder-keeps-application-module/fixtures.components.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'target-component', - template: 'target', -}) -export class TargetComponent {} diff --git a/tests-jest/mock-builder-keeps-application-module/fixtures.modules.ts b/tests-jest/mock-builder-keeps-application-module/fixtures.modules.ts deleted file mode 100644 index dd7e1ea0d1..0000000000 --- a/tests-jest/mock-builder-keeps-application-module/fixtures.modules.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { APP_ID, APP_INITIALIZER, InjectionToken, NgModule } from '@angular/core'; - -import { TargetComponent } from './fixtures.components'; - -export const TARGET_TOKEN = new InjectionToken('TARGET_TOKEN'); - -@NgModule({ - declarations: [TargetComponent], - exports: [TargetComponent], - providers: [ - { - provide: TARGET_TOKEN, - useValue: 'TARGET_TOKEN', - }, - { - provide: APP_ID, - useValue: 'random', - }, - { - multi: true, - provide: APP_INITIALIZER, - useValue: () => undefined, - }, - ], -}) -export class TargetModule {} diff --git a/tests-jest/mock-builder-keeps-application-module/test.spec.ts b/tests-jest/mock-builder-keeps-application-module/test.spec.ts deleted file mode 100644 index a88e147654..0000000000 --- a/tests-jest/mock-builder-keeps-application-module/test.spec.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { APP_ID, APP_INITIALIZER, VERSION } from '@angular/core'; -import { TestBed } from '@angular/core/testing'; -import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; - -import { TargetComponent } from './fixtures.components'; -import { TARGET_TOKEN, TargetModule } from './fixtures.modules'; - -describe('MockBuilderKeepsApplicationModule:real', () => { - beforeEach(() => - TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents() - ); - - it('should render', () => { - const fixture = MockRender(TargetComponent); - const element = ngMocks.find(fixture.debugElement, TargetComponent, undefined); - expect(element).toBeDefined(); - expect(TestBed.get(TARGET_TOKEN)).toBeDefined(); - expect(TestBed.get(APP_INITIALIZER)).toBeDefined(); - expect(TestBed.get(APP_ID)).toBeDefined(); - }); -}); - -describe('MockBuilderKeepsApplicationModule:mock', () => { - beforeEach(() => MockBuilder(TargetComponent, TargetModule)); - - it('should render', () => { - const fixture = MockRender(TargetComponent); - const element = ngMocks.find(fixture.debugElement, TargetComponent); - expect(element).toBeDefined(); - expect(() => TestBed.get(TARGET_TOKEN)).toThrow(); - if (VERSION.major !== '9') { - // somehow ivy doesn't provide APP_INITIALIZER out of the box and this assertion fails. - // our mock logic skips all multi tokens therefore this one isn't present anymore. - expect(TestBed.get(APP_INITIALIZER)).toBeDefined(); - } - expect(TestBed.get(APP_ID)).toBeDefined(); - }); -}); diff --git a/tests-jest/module-with-factory-tokens/fixtures.ts b/tests-jest/module-with-factory-tokens/fixtures.ts deleted file mode 100644 index 3ad0365843..0000000000 --- a/tests-jest/module-with-factory-tokens/fixtures.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { CommonModule } from '@angular/common'; -import { Component, Inject, InjectionToken, NgModule } from '@angular/core'; - -export const MY_TOKEN_SINGLE = new (InjectionToken as any)(/* A5 */ 'MY_TOKEN_SINGLE', { - factory: () => 'MY_TOKEN_SINGLE', -}); - -export const MY_TOKEN_MULTI = new (InjectionToken as any)(/* A5 */ 'MY_TOKEN_MULTI', { - factory: () => 'MY_TOKEN_MULTI', -}); - -@Component({ - selector: 'internal-component', - template: '{{ tokenSingle | json }} {{ tokenMulti | json }}', -}) -export class TargetComponent { - constructor( - @Inject(MY_TOKEN_SINGLE) public readonly tokenSingle: string, - @Inject(MY_TOKEN_MULTI) public readonly tokenMulti: string[] - ) {} -} - -@NgModule({ - declarations: [TargetComponent], - exports: [TargetComponent], - imports: [CommonModule], -}) -export class TargetModule {} diff --git a/tests-jest/module-with-factory-tokens/test.spec.ts b/tests-jest/module-with-factory-tokens/test.spec.ts deleted file mode 100644 index 995bc2e767..0000000000 --- a/tests-jest/module-with-factory-tokens/test.spec.ts +++ /dev/null @@ -1,111 +0,0 @@ -import { VERSION } from '@angular/core'; -import { MockBuilder, MockRender } from 'ng-mocks'; - -import { MY_TOKEN_MULTI, MY_TOKEN_SINGLE, TargetComponent, TargetModule } from './fixtures'; - -// Because all tokens have factories the test should render them correctly. -// There's no way to specify multi in a factory, so we don't get an array. -describe('module-with-factory-tokens:real', () => { - beforeEach(() => MockBuilder().keep(TargetModule)); - - it('renders all tokens', () => { - if (parseInt(VERSION.major, 10) <= 5) { - pending('Need Angular > 5'); - return; - } - - const fixture = MockRender(TargetComponent); - expect(fixture.nativeElement.innerHTML).toEqual( - '"MY_TOKEN_SINGLE" "MY_TOKEN_MULTI"' - ); - }); -}); - -// Because all tokens are kept the test should render them correctly. -// There's no way to specify multi in a factory, so we don't get an array. -describe('module-with-factory-tokens:keep', () => { - beforeEach(() => MockBuilder(TargetComponent, TargetModule).keep(MY_TOKEN_SINGLE).keep(MY_TOKEN_MULTI)); - - it('renders all tokens', () => { - if (parseInt(VERSION.major, 10) <= 5) { - pending('Need Angular > 5'); - return; - } - - const fixture = MockRender(TargetComponent); - expect(fixture.nativeElement.innerHTML).toEqual( - '"MY_TOKEN_SINGLE" "MY_TOKEN_MULTI"' - ); - }); -}); - -// Preferred way. -// Because tokens are provided in the testbed module with custom values the test should render them. -describe('module-with-factory-tokens:mock-0', () => { - beforeEach(() => - MockBuilder(TargetComponent, TargetModule) - .provide({ - provide: MY_TOKEN_SINGLE, - useValue: 'V1', - }) - .provide({ - multi: true, - provide: MY_TOKEN_MULTI, - useValue: 'V2', - }) - ); - - it('fails to render all tokens', () => { - const fixture = MockRender(TargetComponent); - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toContain('"V1" [ "V2" ]'); - }); -}); - -// Because all tokens are mocked in the module the test should render empty values. -// The tokens will be added to provides with undefined values. -// Result of the render is an empty string because there's no way to pass multi. -describe('module-with-factory-tokens:mock-1', () => { - beforeEach(() => MockBuilder(TargetComponent, TargetModule).mock(MY_TOKEN_SINGLE).mock(MY_TOKEN_MULTI)); - - it('renders all tokens', () => { - const fixture = MockRender(TargetComponent); - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toEqual(' '); - }); -}); - -// Because all tokens are mocked with custom values the test should render them. -// There's no way to specify multi in a factory, so we don't get an array. -describe('module-with-factory-tokens:mock-2', () => { - beforeEach(() => - MockBuilder(TargetComponent, TargetModule) - .mock(MY_TOKEN_SINGLE, 'MOCKED_MY_TOKEN_SINGLE') - .mock(MY_TOKEN_MULTI, 'MOCKED_MY_TOKEN_MULTI') - ); - - it('renders all tokens', () => { - const fixture = MockRender(TargetComponent); - expect(fixture.nativeElement.innerHTML).toEqual( - '"MOCKED_MY_TOKEN_SINGLE" "MOCKED_MY_TOKEN_MULTI"' - ); - }); -}); - -// And the most interesting case. Because we don't touch tokens at all and mock the module -// the tokens will used as they are with their factories. -// Unfortunately it's quite tough to guess which tokens we can keep, mocks or omit and now -// a user is responsible to specify tokens for his mock. -describe('module-with-factory-tokens:mock-3', () => { - beforeEach(() => MockBuilder(TargetComponent, TargetModule)); - - it('renders all tokens', () => { - if (parseInt(VERSION.major, 10) <= 5) { - pending('Need Angular > 5'); - return; - } - - const fixture = MockRender(TargetComponent); - expect(fixture.nativeElement.innerHTML).toEqual( - '"MY_TOKEN_SINGLE" "MY_TOKEN_MULTI"' - ); - }); -}); diff --git a/tests-jest/module-with-tokens/fixtures.ts b/tests-jest/module-with-tokens/fixtures.ts deleted file mode 100644 index e39123fde0..0000000000 --- a/tests-jest/module-with-tokens/fixtures.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { CommonModule } from '@angular/common'; -import { Component, Inject, InjectionToken, NgModule } from '@angular/core'; - -export const MY_TOKEN_SINGLE = new InjectionToken('MY_TOKEN_SINGLE'); - -export const MY_TOKEN_MULTI = new InjectionToken('MY_TOKEN_MULTI'); - -@Component({ - selector: 'internal-component', - template: '{{ tokenSingle | json }} {{ tokenMulti | json }}', -}) -export class TargetComponent { - constructor( - @Inject(MY_TOKEN_SINGLE) public readonly tokenSingle: string, - @Inject(MY_TOKEN_MULTI) public readonly tokenMulti: string[] - ) {} -} - -@NgModule({ - declarations: [TargetComponent], - exports: [TargetComponent], - imports: [CommonModule], - providers: [ - { - provide: MY_TOKEN_SINGLE, - useValue: 'MY_TOKEN_SINGLE', - }, - { - multi: true, - provide: MY_TOKEN_MULTI, - useValue: 'MY_TOKEN_MULTI', - }, - { - multi: true, - provide: MY_TOKEN_MULTI, - useValue: 'MY_TOKEN_MULTI_2', - }, - ], -}) -export class TargetModule {} diff --git a/tests-jest/module-with-tokens/test.spec.ts b/tests-jest/module-with-tokens/test.spec.ts deleted file mode 100644 index 22c0984e5c..0000000000 --- a/tests-jest/module-with-tokens/test.spec.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { MockBuilder, MockRender } from 'ng-mocks'; - -import { MY_TOKEN_MULTI, MY_TOKEN_SINGLE, TargetComponent, TargetModule } from './fixtures'; - -// Because all tokens are provided in the module the test should render them correctly. -describe('module-with-tokens:real', () => { - beforeEach(() => MockBuilder().keep(TargetModule)); - - it('renders all tokens', () => { - const fixture = MockRender(TargetComponent); - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toEqual( - '"MY_TOKEN_SINGLE" [ "MY_TOKEN_MULTI", "MY_TOKEN_MULTI_2" ]' - ); - }); -}); - -// Because all tokens are kept in the module the test should render them correctly. -describe('module-with-tokens:keep', () => { - beforeEach(() => MockBuilder(TargetComponent, TargetModule).keep(MY_TOKEN_SINGLE).keep(MY_TOKEN_MULTI)); - - it('renders all tokens', () => { - const fixture = MockRender(TargetComponent); - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toEqual( - '"MY_TOKEN_SINGLE" [ "MY_TOKEN_MULTI", "MY_TOKEN_MULTI_2" ]' - ); - }); -}); - -// Preferred way. -// Because tokens are provided in the testbed module with custom values the test should render them. -describe('module-with-tokens:mock-0', () => { - beforeEach(() => - MockBuilder(TargetComponent, TargetModule) - .provide({ - provide: MY_TOKEN_SINGLE, - useValue: 'V1', - }) - .provide({ - multi: true, - provide: MY_TOKEN_MULTI, - useValue: 'V2', - }) - .provide({ - multi: true, - provide: MY_TOKEN_MULTI, - useValue: 'V3', - }) - ); - - it('fails to render all tokens', () => { - const fixture = MockRender(TargetComponent); - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toEqual( - '"V1" [ "V2", "V3" ]' - ); - }); -}); - -// Because all tokens are mocked in the module the test should render empty values. -// interesting is that for multi it's null, not undefined. -describe('module-with-tokens:mock-1', () => { - beforeEach(() => MockBuilder(TargetComponent, TargetModule).mock(MY_TOKEN_SINGLE).mock(MY_TOKEN_MULTI)); - - it('renders all tokens', () => { - const fixture = MockRender(TargetComponent); - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toEqual( - ' [ null, null ]' - ); - }); -}); - -// Because all tokens are mocked in the module with custom values the test should render them. -describe('module-with-tokens:mock-2', () => { - beforeEach(() => - MockBuilder(TargetComponent, TargetModule) - .mock(MY_TOKEN_SINGLE, 'MOCKED_MY_TOKEN_SINGLE') - .mock(MY_TOKEN_MULTI, 'MOCKED_MY_TOKEN_MULTI') - ); - - it('renders all tokens', () => { - const fixture = MockRender(TargetComponent); - expect(fixture.nativeElement.innerHTML.replace(/\s+/gm, ' ')).toEqual( - '"MOCKED_MY_TOKEN_SINGLE" [ "MOCKED_MY_TOKEN_MULTI", "MOCKED_MY_TOKEN_MULTI" ]' - ); - }); -}); - -// And the most complicated case. Because we don't touch tokens at all and mock the module -// the tokens will be omitted from the final mock and injection will fail. -// Unfortunately it's quite tough to guess which tokens we can keep, mocks or omit and now -// a user is responsible to specify tokens for his mock. -describe('module-with-tokens:mock-3', () => { - beforeEach(() => MockBuilder(TargetComponent, TargetModule)); - - it('fails to render all tokens', () => { - expect(() => MockRender(TargetComponent)).toThrowError(/InjectionToken/); - }); -}); diff --git a/tests-jest/nested-before-each/fixtures.components.ts b/tests-jest/nested-before-each/fixtures.components.ts deleted file mode 100644 index 9fd4b87b11..0000000000 --- a/tests-jest/nested-before-each/fixtures.components.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'internal-component', - template: 'internal', -}) -export class InternalComponent {} diff --git a/tests-jest/nested-before-each/fixtures.modules.ts b/tests-jest/nested-before-each/fixtures.modules.ts deleted file mode 100644 index 430a37d058..0000000000 --- a/tests-jest/nested-before-each/fixtures.modules.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { NgModule } from '@angular/core'; - -import { InternalComponent } from './fixtures.components'; - -@NgModule({ - declarations: [InternalComponent], - exports: [InternalComponent], -}) -export class TargetModule {} diff --git a/tests-jest/nested-before-each/test.spec.ts b/tests-jest/nested-before-each/test.spec.ts deleted file mode 100644 index 1aa18a7b6a..0000000000 --- a/tests-jest/nested-before-each/test.spec.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { Type } from '@angular/core'; -import { TestBed } from '@angular/core/testing'; -import { MockComponent } from 'ng-mocks'; - -import { InternalComponent } from './fixtures.components'; - -describe('nested-before-each', () => { - let level = 0; - let mock: Type; - - beforeEach(() => { - level = 0; - mock = MockComponent(InternalComponent); - TestBed.configureTestingModule({ - declarations: [mock], - }); - return TestBed.compileComponents(); - }); - - describe('tested', () => { - beforeEach(() => { - level += 1; - }); - - describe('tested', () => { - beforeEach(() => { - level += 1; - }); - - it('should have the same mock after the first run', () => { - expect(level).toBeGreaterThan(0); - expect(MockComponent(InternalComponent)).toBe(mock); - }); - - it('should have the same mock after the second run', () => { - expect(level).toBeGreaterThan(0); - expect(MockComponent(InternalComponent)).toBe(mock); - }); - }); - }); -}); - -describe('nested-before-all', () => { - let level = 0; - let mock: Type; - - beforeAll(() => { - level = 0; - mock = MockComponent(InternalComponent); - TestBed.resetTestingModule(); - TestBed.configureTestingModule({ - declarations: [mock], - }); - return TestBed.compileComponents(); - }); - - describe('tested', () => { - beforeEach(() => { - level += 1; - }); - - describe('tested', () => { - beforeEach(() => { - level += 1; - }); - - it('should have the same mock after the first run', () => { - expect(level).toBeGreaterThan(0); - expect(MockComponent(InternalComponent)).toBe(mock); - }); - - it('should have the same mock after the second run', () => { - expect(level).toBeGreaterThan(0); - expect(MockComponent(InternalComponent)).toBe(mock); - }); - }); - }); -}); diff --git a/tests-jest/normal-usage-after-mock-builder/fixtures.components.ts b/tests-jest/normal-usage-after-mock-builder/fixtures.components.ts deleted file mode 100644 index 78929f1109..0000000000 --- a/tests-jest/normal-usage-after-mock-builder/fixtures.components.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { Component } from '@angular/core'; - -import { TargetService } from './fixtures.services'; - -@Component({ - selector: 'root', - template: '{{ service.called }}', -}) -export class TargetComponent { - public readonly service: TargetService; - - constructor(service: TargetService) { - this.service = service; - } -} - -@Component({ - selector: 'internal', - template: 'real', -}) -export class RealComponent {} - -@Component({ - selector: 'internal', - template: 'fake', -}) -export class FakeComponent {} diff --git a/tests-jest/normal-usage-after-mock-builder/fixtures.modules.ts b/tests-jest/normal-usage-after-mock-builder/fixtures.modules.ts deleted file mode 100644 index dcf1791fe4..0000000000 --- a/tests-jest/normal-usage-after-mock-builder/fixtures.modules.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { NgModule } from '@angular/core'; - -import { RealComponent, TargetComponent } from './fixtures.components'; -import { TargetService } from './fixtures.services'; - -@NgModule({ - declarations: [TargetComponent, RealComponent], - exports: [TargetComponent], - providers: [TargetService], -}) -export class TargetModule { - protected service: TargetService; - - constructor(service: TargetService) { - this.service = service; - this.service.call(); - } -} diff --git a/tests-jest/normal-usage-after-mock-builder/fixtures.services.ts b/tests-jest/normal-usage-after-mock-builder/fixtures.services.ts deleted file mode 100644 index 1dcb2be9d6..0000000000 --- a/tests-jest/normal-usage-after-mock-builder/fixtures.services.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Injectable } from '@angular/core'; - -@Injectable() -export class TargetService { - public called = 0; - - public call(): void { - this.called += 1; - } -} diff --git a/tests-jest/on-push/on-push.spec.ts b/tests-jest/on-push/on-push.spec.ts deleted file mode 100644 index 0bdd8b96fe..0000000000 --- a/tests-jest/on-push/on-push.spec.ts +++ /dev/null @@ -1,95 +0,0 @@ -import { ChangeDetectionStrategy, Component, Input } from '@angular/core'; -import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { MockedDebugElement, MockRender, ngMocks } from 'ng-mocks'; - -@Component({ - changeDetection: ChangeDetectionStrategy.OnPush, - selector: 'item-list', - template: '{{items.length}}', -}) -export class ItemListComponent { - @Input() items: string[]; -} - -@Component({ - selector: 'item-list-wrapper', - template: '', -}) -export class ItemListWrapperComponent { - @Input() items: string[]; -} - -describe('ChangeDetectionStrategy.OnPush:real', () => { - let wrapper: ComponentFixture; - let componentDebugElement: MockedDebugElement; - - beforeEach(() => { - // const wrapperType = WrapComponent(ItemListComponent); - TestBed.configureTestingModule({ - declarations: [ - ItemListComponent, - ItemListWrapperComponent, - // wrapperType, - ], - }); - - wrapper = TestBed.createComponent(ItemListWrapperComponent); - wrapper.componentInstance.items = []; - wrapper.detectChanges(); - - componentDebugElement = ngMocks.find(wrapper.debugElement, ItemListComponent); - }); - - it('should show 0 if no items', () => { - expect(componentDebugElement.nativeElement.innerHTML).toEqual('0'); - }); - - it('should show 0 if items pushed to array but not changed reference', () => { - wrapper.componentInstance.items.push('demo'); - wrapper.detectChanges(); - - expect(componentDebugElement.nativeElement.innerHTML).toEqual('0'); - }); - - it('should show 1 if items array changed reference', () => { - wrapper.componentInstance.items = ['demo']; - wrapper.detectChanges(); - - expect(componentDebugElement.nativeElement.innerHTML).toEqual('1'); - }); -}); - -describe('ChangeDetectionStrategy.OnPush:mock', () => { - let fixture: ComponentFixture; - let component: ItemListComponent; - - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [ItemListComponent], - }); - - fixture = MockRender(ItemListComponent, { - items: [], - }); - component = fixture.componentInstance; - fixture.detectChanges(); - }); - - it('should show 0 if no items', () => { - expect(fixture.nativeElement.innerHTML).toContain('>0<'); - }); - - it('should show 0 if items pushed to array but not changed reference', () => { - component.items.push('demo'); - fixture.detectChanges(); - - expect(fixture.nativeElement.innerHTML).toContain('>0<'); - }); - - it('should show 1 if items array changed reference', () => { - component.items = ['demo']; - fixture.detectChanges(); - - expect(fixture.nativeElement.innerHTML).toContain('>1<'); - }); -}); diff --git a/tests-jest/provider-with-dependency/fixtures.components.ts b/tests-jest/provider-with-dependency/fixtures.components.ts deleted file mode 100644 index ec2df5b5c2..0000000000 --- a/tests-jest/provider-with-dependency/fixtures.components.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Component } from '@angular/core'; - -import { ServiceChild } from './fixtures.services'; - -@Component({ - selector: 'internal-component', - template: '{{ child.parent.echo() }}', -}) -export class InternalComponent { - public readonly child: ServiceChild; - - constructor(child: ServiceChild) { - this.child = child; - } -} diff --git a/tests-jest/provider-with-dependency/fixtures.modules.ts b/tests-jest/provider-with-dependency/fixtures.modules.ts deleted file mode 100644 index ec07026ec8..0000000000 --- a/tests-jest/provider-with-dependency/fixtures.modules.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { NgModule } from '@angular/core'; - -import { InternalComponent } from './fixtures.components'; -import { ServiceChild, ServiceParent, ServiceReplacedParent } from './fixtures.services'; - -@NgModule({ - declarations: [InternalComponent], - exports: [InternalComponent], - providers: [ - ServiceParent, - ServiceReplacedParent, - { - deps: [ServiceReplacedParent], - provide: ServiceChild, - useFactory: (parent: ServiceParent) => new ServiceChild(parent), - }, - ], -}) -export class TargetModule {} diff --git a/tests-jest/provider-with-dependency/fixtures.services.ts b/tests-jest/provider-with-dependency/fixtures.services.ts deleted file mode 100644 index 15266dde1d..0000000000 --- a/tests-jest/provider-with-dependency/fixtures.services.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Injectable } from '@angular/core'; - -@Injectable() -export class ServiceParent { - protected value = 'parent'; - - public echo() { - return this.value; - } -} - -@Injectable() -export class ServiceReplacedParent extends ServiceParent { - protected value = 'replaced'; -} - -@Injectable() -export class ServiceChild { - public readonly parent: ServiceParent; - - constructor(parent: ServiceParent) { - this.parent = parent; - } -} diff --git a/tests-jest/provider-with-dependency/test.spec.ts b/tests-jest/provider-with-dependency/test.spec.ts deleted file mode 100644 index 7720fc7457..0000000000 --- a/tests-jest/provider-with-dependency/test.spec.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { Injectable } from '@angular/core'; -import { TestBed } from '@angular/core/testing'; -import { MockBuilder, MockRender } from 'ng-mocks'; - -import { InternalComponent } from './fixtures.components'; -import { TargetModule } from './fixtures.modules'; -import { ServiceReplacedParent } from './fixtures.services'; - -@Injectable() -class ServiceMock { - protected value = 'mock'; - - public echo() { - return this.value; - } -} - -describe('provider-with-dependency:real', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - imports: [TargetModule], - }); - return TestBed.compileComponents(); - }); - - it('should render "parent"', () => { - const fixture = MockRender(InternalComponent); - expect(fixture.debugElement.nativeElement.innerHTML).toEqual('replaced'); - }); -}); - -describe('provider-with-dependency:provided', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - imports: [TargetModule], - providers: [ - { - provide: ServiceReplacedParent, - useClass: ServiceMock, - }, - ], - }); - return TestBed.compileComponents(); - }); - - it('should render "parent"', () => { - const fixture = MockRender(InternalComponent); - expect(fixture.debugElement.nativeElement.innerHTML).toEqual('mock'); - }); -}); - -describe('provider-with-dependency:mock', () => { - beforeEach(() => { - const ngModule = MockBuilder() - .keep(TargetModule) - .provide({ - provide: ServiceReplacedParent, - useClass: ServiceMock, - }) - .build(); - TestBed.configureTestingModule(ngModule); - return TestBed.compileComponents(); - }); - - it('should render "parent" even the providers where patched', () => { - const fixture = MockRender(InternalComponent); - expect(fixture.debugElement.nativeElement.innerHTML).toEqual('mock'); - }); -}); diff --git a/tests-jest/rerender-rendered-content-child/fixtures.components.ts b/tests-jest/rerender-rendered-content-child/fixtures.components.ts deleted file mode 100644 index 53bac0df8a..0000000000 --- a/tests-jest/rerender-rendered-content-child/fixtures.components.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Component, ContentChild, TemplateRef } from '@angular/core'; - -import { staticFalse } from '..'; - -@Component({ - selector: 'ccc', - template: ``, -}) -export class ContentChildComponent { - @ContentChild('block', { ...staticFalse }) injectedBlock: TemplateRef; -} diff --git a/tests-jest/rerender-rendered-content-child/fixtures.module.ts b/tests-jest/rerender-rendered-content-child/fixtures.module.ts deleted file mode 100644 index df543f0a0c..0000000000 --- a/tests-jest/rerender-rendered-content-child/fixtures.module.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { CommonModule } from '@angular/common'; -import { NgModule } from '@angular/core'; - -import { ContentChildComponent } from './fixtures.components'; - -@NgModule({ - declarations: [ContentChildComponent], - exports: [ContentChildComponent], - imports: [CommonModule], -}) -export class ContentChildModule {} diff --git a/tests-jest/rerender-rendered-content-child/test.spec.ts b/tests-jest/rerender-rendered-content-child/test.spec.ts deleted file mode 100644 index 0d05a76dcd..0000000000 --- a/tests-jest/rerender-rendered-content-child/test.spec.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { MockBuilder, MockComponent, MockRender, ngMocks } from 'ng-mocks'; - -import { ContentChildComponent } from './fixtures.components'; - -describe('Rerender of a rendered @ContentChild', () => { - beforeEach(async () => { - await MockBuilder().mock(ContentChildComponent, { - render: { - block: { - $implicit: '$implicit', - }, - }, - }); - }); - - it('should rerender everything correctly', () => { - const fixture = MockRender( - ` - {{ value }} {{ outside }} - `, - { - outside: '1', - } - ); - expect(fixture).toBeDefined(); - expect(fixture.debugElement.nativeElement.innerHTML).toContain('$implicit 1'); - - fixture.componentInstance.outside = '2'; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement.innerHTML).toContain('$implicit 2'); - - const component = ngMocks.find(fixture.debugElement, MockComponent(ContentChildComponent)).componentInstance; - - component.__render('block', 'updated'); - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement.innerHTML).toContain('updated 2'); - - fixture.componentInstance.outside = '3'; - fixture.detectChanges(); - expect(fixture.debugElement.nativeElement.innerHTML).toContain('updated 3'); - }); -}); diff --git a/tests-jest/shared-mocked-module/fixtures.components.ts b/tests-jest/shared-mocked-module/fixtures.components.ts deleted file mode 100644 index 16c311267e..0000000000 --- a/tests-jest/shared-mocked-module/fixtures.components.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Component } from '@angular/core'; - -@Component({ - selector: 'my-component', - template: 'real content', -}) -export class MyComponent {} - -@Component({ - selector: 'child-1-component', - template: 'child:1 ', -}) -export class Child1Component {} - -@Component({ - selector: 'child-2-component', - template: 'child:2 ', -}) -export class Child2Component {} - -@Component({ - selector: 'target-component', - template: ' - ', -}) -export class TargetComponent {} diff --git a/tests-jest/shared-mocked-module/fixtures.modules.ts b/tests-jest/shared-mocked-module/fixtures.modules.ts deleted file mode 100644 index 23252afd6b..0000000000 --- a/tests-jest/shared-mocked-module/fixtures.modules.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { CommonModule } from '@angular/common'; -import { NgModule } from '@angular/core'; - -import { Child1Component, Child2Component, MyComponent, TargetComponent } from './fixtures.components'; - -@NgModule({ - declarations: [MyComponent], - exports: [MyComponent], -}) -export class MyModule {} - -@NgModule({ - declarations: [Child1Component], - exports: [Child1Component], - imports: [MyModule], -}) -export class Child1Module {} - -@NgModule({ - declarations: [Child2Component], - exports: [Child2Component], - imports: [MyModule], -}) -export class Child2Module {} - -@NgModule({ - declarations: [TargetComponent], - exports: [TargetComponent], - imports: [CommonModule, Child1Module, Child2Module], -}) -export class TargetModule {} diff --git a/tests-jest/shared-mocked-module/test.spec.ts b/tests-jest/shared-mocked-module/test.spec.ts deleted file mode 100644 index 9aa9952194..0000000000 --- a/tests-jest/shared-mocked-module/test.spec.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { TestBed } from '@angular/core/testing'; -import { MockBuilder, MockComponent, MockRender, ngMocks } from 'ng-mocks'; - -import { MyComponent, TargetComponent } from './fixtures.components'; -import { TargetModule } from './fixtures.modules'; - -describe('SharedMockedModule:real', () => { - beforeEach(async done => { - await TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents(); - done(); - }); - - it('should render', () => { - const fixture = MockRender(TargetComponent); - expect(fixture).toBeDefined(); - const content = fixture.debugElement.nativeElement.innerHTML; - expect(content).toContain( - 'child:1 real content' - ); - expect(content).toContain( - 'child:2 real content' - ); - }); -}); - -describe('SharedMockedModule:mock', () => { - beforeEach(async done => { - await MockBuilder(TargetComponent).keep(TargetModule).mock(MyComponent); - done(); - }); - - // The expectation is to verify that only MyComponent was mocked, even it was deeply nested. - it('should render', () => { - const fixture = MockRender(TargetComponent); - expect(fixture).toBeDefined(); - const content = fixture.debugElement.nativeElement.innerHTML; - const component = ngMocks.find(fixture.debugElement, MockComponent(MyComponent)).componentInstance; - expect(component).toBeDefined(); - expect(content).toContain('child:1 '); - expect(content).toContain('child:2 '); - }); -}); diff --git a/tests-jest/spies/fixtures.components.ts b/tests-jest/spies/fixtures.components.ts deleted file mode 100644 index 3d0e50079f..0000000000 --- a/tests-jest/spies/fixtures.components.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Component } from '@angular/core'; - -import { TargetService } from './fixtures.providers'; - -@Component({ - selector: 'target', - template: '', -}) -export class TargetComponent { - protected service: TargetService; - - constructor(service: TargetService) { - this.service = service; - this.service.echo('constructor'); - } - - public echo(): string { - return this.service.echo('TargetComponent'); - } -} diff --git a/tests-jest/spies/fixtures.modules.ts b/tests-jest/spies/fixtures.modules.ts deleted file mode 100644 index 7f262d7038..0000000000 --- a/tests-jest/spies/fixtures.modules.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { NgModule } from '@angular/core'; - -import { TargetService } from './fixtures.providers'; - -@NgModule({ - providers: [TargetService], -}) -export class TargetModule {} diff --git a/tests-jest/spies/fixtures.providers.ts b/tests-jest/spies/fixtures.providers.ts deleted file mode 100644 index ed3362df27..0000000000 --- a/tests-jest/spies/fixtures.providers.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Injectable } from '@angular/core'; - -@Injectable() -export class TargetService { - protected value = 'TargetService'; - - public echo(value?: string): string { - return value ? value : this.value; - } -} diff --git a/tests-jest/structural-directives/custom-ng-for-with-of.directive.ts b/tests-jest/structural-directives/custom-ng-for-with-of.directive.ts deleted file mode 100644 index 5919cf22c1..0000000000 --- a/tests-jest/structural-directives/custom-ng-for-with-of.directive.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core'; - -export interface ICustomNgForContext { - $implicit: string; // real value - myFirst: boolean; - myIndex: number; - myLast: boolean; -} - -@Directive({ - selector: '[customNgForWithOf]', -}) -export class CustomNgForWithOfDirective { - @Input('customNgForWithOfOf') set setItems(items: string[]) { - this.viewContainerRef.clear(); - - items.forEach((value, index) => - this.viewContainerRef.createEmbeddedView(this.templateRef, { - $implicit: value, - myFirst: index === 0, - myIndex: index, - myLast: index + 1 === items.length, - }) - ); - } - - protected templateRef: TemplateRef; - protected viewContainerRef: ViewContainerRef; - - constructor(templateRef: TemplateRef, viewContainerRef: ViewContainerRef) { - this.templateRef = templateRef; - this.viewContainerRef = viewContainerRef; - } -} diff --git a/tests-jest/structural-directives/custom-ng-for-without-of.directive.ts b/tests-jest/structural-directives/custom-ng-for-without-of.directive.ts deleted file mode 100644 index 618fcbfecd..0000000000 --- a/tests-jest/structural-directives/custom-ng-for-without-of.directive.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core'; - -export interface ICustomNgForContext { - $implicit: string; // real value - myFirst: boolean; - myIndex: number; - myLast: boolean; -} - -@Directive({ - selector: '[customNgForWithoutOf]', -}) -export class CustomNgForWithoutOfDirective { - @Input('customNgForWithoutOf') set setItems(items: string[]) { - this.viewContainerRef.clear(); - - items.forEach((value, index) => - this.viewContainerRef.createEmbeddedView(this.templateRef, { - $implicit: value, - myFirst: index === 0, - myIndex: index, - myLast: index + 1 === items.length, - }) - ); - } - - protected templateRef: TemplateRef; - protected viewContainerRef: ViewContainerRef; - - constructor(templateRef: TemplateRef, viewContainerRef: ViewContainerRef) { - this.templateRef = templateRef; - this.viewContainerRef = viewContainerRef; - } -} diff --git a/tests-jest/structural-directives/custom-ng-if.directive.ts b/tests-jest/structural-directives/custom-ng-if.directive.ts deleted file mode 100644 index 2158c1d72a..0000000000 --- a/tests-jest/structural-directives/custom-ng-if.directive.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core'; - -@Directive({ - selector: '[customNgIf]', -}) -export class CustomNgIfDirective { - @Input('customNgIf') set setValue(value: any) { - if (value) { - this.viewContainerRef.createEmbeddedView(this.templateRef); - } else { - this.viewContainerRef.clear(); - } - } - - protected templateRef: TemplateRef; - protected viewContainerRef: ViewContainerRef; - - constructor(templateRef: TemplateRef, viewContainerRef: ViewContainerRef) { - this.templateRef = templateRef; - this.viewContainerRef = viewContainerRef; - } -} diff --git a/tests-jest/context-with-directives/context-with-directives.spec.ts b/tests/context-with-directives/context-with-directives.spec.ts similarity index 86% rename from tests-jest/context-with-directives/context-with-directives.spec.ts rename to tests/context-with-directives/context-with-directives.spec.ts index c79db7edcb..4f0d8f1e56 100644 --- a/tests-jest/context-with-directives/context-with-directives.spec.ts +++ b/tests/context-with-directives/context-with-directives.spec.ts @@ -1,19 +1,11 @@ import { CommonModule } from '@angular/common'; -import { TestBed } from '@angular/core/testing'; -import { MockDirective, MockRender, ngMocks } from 'ng-mocks'; +import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; import { CustomRootComponent } from './custom-root.component'; import { CustomTypeDirective } from './custom-type.directive'; describe('context-with-directives:real', () => { - beforeEach(done => { - TestBed.configureTestingModule({ - declarations: [CustomTypeDirective, CustomRootComponent], - imports: [CommonModule], - }) - .compileComponents() - .then(done); - }); + beforeEach(() => MockBuilder().keep(CustomTypeDirective).keep(CustomRootComponent).keep(CommonModule)); it('renders everything right', () => { const fixture = MockRender(` @@ -68,13 +60,7 @@ describe('context-with-directives:real', () => { }); describe('context-with-directives:mock', () => { - beforeEach(done => { - TestBed.configureTestingModule({ - declarations: [MockDirective(CustomTypeDirective), MockDirective(CustomRootComponent)], - }) - .compileComponents() - .then(done); - }); + beforeEach(() => MockBuilder().mock(CustomTypeDirective).mock(CustomRootComponent)); it('renders everything what is not template', () => { const fixture = MockRender(` diff --git a/tests-jasmine/context-with-directives/custom-root.component.ts b/tests/context-with-directives/custom-root.component.ts similarity index 100% rename from tests-jasmine/context-with-directives/custom-root.component.ts rename to tests/context-with-directives/custom-root.component.ts diff --git a/tests-jasmine/context-with-directives/custom-type.directive.ts b/tests/context-with-directives/custom-type.directive.ts similarity index 100% rename from tests-jasmine/context-with-directives/custom-type.directive.ts rename to tests/context-with-directives/custom-type.directive.ts diff --git a/tests-jasmine/control-value-accessor-form-control/fixtures.ts b/tests/control-value-accessor-form-control/fixtures.ts similarity index 100% rename from tests-jasmine/control-value-accessor-form-control/fixtures.ts rename to tests/control-value-accessor-form-control/fixtures.ts diff --git a/tests-jest/control-value-accessor-form-control/test.spec.ts b/tests/control-value-accessor-form-control/test.spec.ts similarity index 90% rename from tests-jest/control-value-accessor-form-control/test.spec.ts rename to tests/control-value-accessor-form-control/test.spec.ts index 562ffc7c30..5481a79229 100644 --- a/tests-jest/control-value-accessor-form-control/test.spec.ts +++ b/tests/control-value-accessor-form-control/test.spec.ts @@ -1,5 +1,5 @@ import { ReactiveFormsModule } from '@angular/forms'; -import { MockBuilder, MockComponent, MockHelper, MockRender } from 'ng-mocks'; +import { MockBuilder, MockComponent, MockRender, ngMocks } from 'ng-mocks'; import { ControlComponent, TargetComponent, TargetModule } from './fixtures'; @@ -9,7 +9,7 @@ describe('control-value-accessor-form-control:real', () => { it('respects our formControl', () => { const fixture = MockRender(TargetComponent, {}, false); - const mock = MockHelper.findOrFail(fixture.debugElement, ControlComponent).componentInstance; + const mock = ngMocks.find(fixture.debugElement, ControlComponent).componentInstance; spyOn(mock, 'writeValue').and.callThrough(); spyOn(mock, 'setDisabledState').and.callThrough(); fixture.detectChanges(); @@ -56,7 +56,7 @@ describe('control-value-accessor-form-control:mock', () => { it('respects our formControl', () => { const fixture = MockRender(TargetComponent, {}, false); - const mock = MockHelper.findOrFail(fixture.debugElement, MockComponent(ControlComponent)).componentInstance; + const mock = ngMocks.find(fixture.debugElement, MockComponent(ControlComponent)).componentInstance; spyOn(mock, 'writeValue').and.callThrough(); spyOn(mock, 'setDisabledState').and.callThrough(); spyOn(mock, 'registerOnChange').and.callThrough(); @@ -82,7 +82,7 @@ describe('control-value-accessor-form-control:mock', () => { fixture.point.componentInstance.control.markAsUntouched(); expect(fixture.point.componentInstance.control.touched).toBeFalsy(); // a way through a spy - MockHelper.mockService(mock, 'registerOnTouched').calls.first().args[0](); + ngMocks.stub(mock, 'registerOnTouched').calls.first().args[0](); expect(fixture.point.componentInstance.control.touched).toBeTruthy(); fixture.point.componentInstance.control.markAsUntouched(); @@ -99,7 +99,7 @@ describe('control-value-accessor-form-control:mock', () => { expect(fixture.point.componentInstance.control.touched).toBeFalsy(); expect(fixture.point.componentInstance.control.value).toBe('test3'); // a way through a spy - MockHelper.mockService(mock, 'registerOnChange').calls.first().args[0]('test4'); + ngMocks.stub(mock, 'registerOnChange').calls.first().args[0]('test4'); expect(mock.writeValue).not.toHaveBeenCalledWith('test4'); expect(fixture.point.componentInstance.control.touched).toBeFalsy(); expect(fixture.point.componentInstance.control.value).toBe('test4'); diff --git a/tests-jasmine/control-value-accessor-ng-model/fixtures.ts b/tests/control-value-accessor-ng-model/fixtures.ts similarity index 100% rename from tests-jasmine/control-value-accessor-ng-model/fixtures.ts rename to tests/control-value-accessor-ng-model/fixtures.ts diff --git a/tests-jasmine/control-value-accessor-ng-model/test.spec.ts b/tests/control-value-accessor-ng-model/test.spec.ts similarity index 88% rename from tests-jasmine/control-value-accessor-ng-model/test.spec.ts rename to tests/control-value-accessor-ng-model/test.spec.ts index 0817a7e3b9..ce57c71ad9 100644 --- a/tests-jasmine/control-value-accessor-ng-model/test.spec.ts +++ b/tests/control-value-accessor-ng-model/test.spec.ts @@ -1,5 +1,5 @@ import { FormsModule, NgModel } from '@angular/forms'; -import { MockBuilder, MockComponent, MockHelper, MockRender } from 'ng-mocks'; +import { MockBuilder, MockComponent, MockRender, ngMocks } from 'ng-mocks'; import { ControlComponent, TargetComponent, TargetModule } from './fixtures'; @@ -9,11 +9,11 @@ describe('control-value-accessor-ng-model:real', () => { it('respects our ngModel', async () => { const fixture = MockRender(TargetComponent, {}, false); - const mockElement = MockHelper.findOrFail(fixture.debugElement, ControlComponent); + const mockElement = ngMocks.find(fixture.debugElement, ControlComponent); const mock = mockElement.componentInstance; spyOn(mock, 'writeValue').and.callThrough(); spyOn(mock, 'setDisabledState').and.callThrough(); - const ngModel = MockHelper.getDirectiveOrFail(mockElement, NgModel); + const ngModel = ngMocks.get(mockElement, NgModel); fixture.detectChanges(); await fixture.whenStable(); @@ -67,13 +67,13 @@ describe('control-value-accessor-ng-model:mock', () => { it('respects our ngModel', async () => { const fixture = MockRender(TargetComponent, {}, false); - const mockElement = MockHelper.findOrFail(fixture.debugElement, MockComponent(ControlComponent)); + const mockElement = ngMocks.find(fixture.debugElement, MockComponent(ControlComponent)); const mock = mockElement.componentInstance; spyOn(mock, 'writeValue').and.callThrough(); spyOn(mock, 'setDisabledState').and.callThrough(); spyOn(mock, 'registerOnChange').and.callThrough(); spyOn(mock, 'registerOnTouched').and.callThrough(); - const ngModel = MockHelper.getDirectiveOrFail(mockElement, NgModel); + const ngModel = ngMocks.get(mockElement, NgModel); fixture.detectChanges(); await fixture.whenStable(); @@ -100,7 +100,7 @@ describe('control-value-accessor-ng-model:mock', () => { ngModel.control.markAsUntouched(); expect(ngModel.touched).toBeFalsy(); // a way through a spy - MockHelper.mockService(mock, 'registerOnTouched').calls.first().args[0](); + ngMocks.stub(mock, 'registerOnTouched').calls.first().args[0](); expect(ngModel.touched).toBeTruthy(); ngModel.control.markAsUntouched(); @@ -121,7 +121,7 @@ describe('control-value-accessor-ng-model:mock', () => { expect(ngModel.touched).toBeFalsy(); expect(fixture.point.componentInstance.value).toBe('test3'); // a way through a spy - MockHelper.mockService(mock, 'registerOnChange').calls.first().args[0]('test4'); + ngMocks.stub(mock, 'registerOnChange').calls.first().args[0]('test4'); expect(mock.writeValue).not.toHaveBeenCalledWith('test4'); expect(ngModel.touched).toBeFalsy(); expect(ngModel.value).toBe('test4'); diff --git a/tests-jasmine/exports-only/fixtures.components.ts b/tests/exports-only/fixtures.components.ts similarity index 100% rename from tests-jasmine/exports-only/fixtures.components.ts rename to tests/exports-only/fixtures.components.ts diff --git a/tests-jasmine/exports-only/fixtures.modules.ts b/tests/exports-only/fixtures.modules.ts similarity index 100% rename from tests-jasmine/exports-only/fixtures.modules.ts rename to tests/exports-only/fixtures.modules.ts diff --git a/tests-jest/exports-only/test.spec.ts b/tests/exports-only/test.spec.ts similarity index 90% rename from tests-jest/exports-only/test.spec.ts rename to tests/exports-only/test.spec.ts index cbe0a3d431..76c0d9a32a 100644 --- a/tests-jest/exports-only/test.spec.ts +++ b/tests/exports-only/test.spec.ts @@ -1,16 +1,10 @@ -import { TestBed } from '@angular/core/testing'; import { MockBuilder, MockRender } from 'ng-mocks'; import { InternalComponent } from './fixtures.components'; import { TargetModule } from './fixtures.modules'; describe('ExportsOnly:real', () => { - beforeEach(async done => { - await TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents(); - done(); - }); + beforeEach(() => MockBuilder(TargetModule)); it('should render', () => { const fixture = MockRender(InternalComponent); diff --git a/tests-jasmine/get-inputs-and-outputs/fixtures.ts b/tests/get-inputs-and-outputs/fixtures.ts similarity index 100% rename from tests-jasmine/get-inputs-and-outputs/fixtures.ts rename to tests/get-inputs-and-outputs/fixtures.ts diff --git a/tests-jasmine/get-inputs-and-outputs/test.spec.ts b/tests/get-inputs-and-outputs/test.spec.ts similarity index 62% rename from tests-jasmine/get-inputs-and-outputs/test.spec.ts rename to tests/get-inputs-and-outputs/test.spec.ts index 0b664bee30..0c60b4e42f 100644 --- a/tests-jasmine/get-inputs-and-outputs/test.spec.ts +++ b/tests/get-inputs-and-outputs/test.spec.ts @@ -1,22 +1,21 @@ -import { TestBed } from '@angular/core/testing'; -import { MockRender, ngMocks } from 'ng-mocks'; +import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; import { Target2Directive, Target3Directive, TargetComponent, TargetModule } from './fixtures'; +// fix to support both jasmine and jest in the test +declare const jest: any; +declare const jasmine: any; + describe('get-inputs-and-outputs', () => { - beforeEach(() => - TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents() - ); + beforeEach(() => MockBuilder(TargetModule)); it('finds them correctly', () => { const params = { input1: '1', input2: '2', - output1: jasmine.createSpy('output1'), - output2: jasmine.createSpy('output2'), - output3: jasmine.createSpy('output3'), + output1: typeof jest !== 'undefined' ? jest.fn().mockName('output1') : jasmine.createSpy('output1'), + output2: typeof jest !== 'undefined' ? jest.fn().mockName('output2') : jasmine.createSpy('output2'), + output3: typeof jest !== 'undefined' ? jest.fn().mockName('output3') : jasmine.createSpy('output3'), }; const fixture = MockRender( ` { const directive3 = ngMocks.get(componentElement, Target3Directive); expect(component.input).toEqual('1'); - params.output1.calls.reset(); + if (params.output1.mockReset) { + params.output1.mockReset(); + } else { + params.output1.calls.reset(); + } component.output.emit(); expect(params.output1).toHaveBeenCalled(); expect(directive2.input).toEqual('2'); expect(directive2.input2).toEqual(undefined); - params.output2.calls.reset(); + if (params.output2.mockReset) { + params.output2.mockReset(); + } else { + params.output2.calls.reset(); + } directive2.output.emit(); expect(params.output2).toHaveBeenCalled(); expect(directive3.input).toEqual('3'); - params.output3.calls.reset(); + if (params.output3.mockReset) { + params.output3.mockReset(); + } else { + params.output3.calls.reset(); + } directive3.output.emit(); expect(params.output3).toHaveBeenCalled(); @@ -59,13 +70,26 @@ describe('get-inputs-and-outputs', () => { 'Cannot find inputUndefined input via ngMocks.input' ); expect(ngMocks.input(componentElement, 'input3')).toEqual('3'); - params.output1.calls.reset(); + if (params.output1.mockReset) { + params.output1.mockReset(); + } else { + params.output1.calls.reset(); + } ngMocks.output(componentElement, 'output1').emit(); expect(params.output1).toHaveBeenCalled(); - params.output2.calls.reset(); + if (params.output2.mockReset) { + params.output2.mockReset(); + } else { + params.output2.calls.reset(); + } + ngMocks.output(componentElement, 'output2').emit(); expect(params.output2).toHaveBeenCalled(); - params.output3.calls.reset(); + if (params.output3.mockReset) { + params.output3.mockReset(); + } else { + params.output3.calls.reset(); + } ngMocks.output(componentElement, 'output3').emit(); expect(params.output3).toHaveBeenCalled(); expect(() => ngMocks.output(componentElement, 'outputUndefined')).toThrowError( diff --git a/tests-jasmine/index.ts b/tests/index.ts similarity index 100% rename from tests-jasmine/index.ts rename to tests/index.ts diff --git a/tests-jasmine/injected-ng-templates/custom-injection.component.ts b/tests/injected-ng-templates/custom-injection.component.ts similarity index 100% rename from tests-jasmine/injected-ng-templates/custom-injection.component.ts rename to tests/injected-ng-templates/custom-injection.component.ts diff --git a/tests-jest/injected-ng-templates/injected-ng-templates.spec.ts b/tests/injected-ng-templates/injected-ng-templates.spec.ts similarity index 87% rename from tests-jest/injected-ng-templates/injected-ng-templates.spec.ts rename to tests/injected-ng-templates/injected-ng-templates.spec.ts index 946bf4e286..bf29757804 100644 --- a/tests-jest/injected-ng-templates/injected-ng-templates.spec.ts +++ b/tests/injected-ng-templates/injected-ng-templates.spec.ts @@ -1,15 +1,10 @@ import { DebugElement } from '@angular/core'; -import { TestBed } from '@angular/core/testing'; -import { MockComponent, MockRender, ngMocks } from 'ng-mocks'; +import { MockBuilder, MockComponent, MockRender, ngMocks } from 'ng-mocks'; import { CustomInjectionComponent } from './custom-injection.component'; describe('injected-ng-templates:real', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [CustomInjectionComponent], - }); - }); + beforeEach(() => MockBuilder(CustomInjectionComponent)); it('renders injected template properly', () => { // should iterate against 3 string. @@ -32,11 +27,7 @@ describe('injected-ng-templates:real', () => { }); describe('structural-directive-as-ng-for:mock', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [MockComponent(CustomInjectionComponent)], - }); - }); + beforeEach(() => MockBuilder().mock(CustomInjectionComponent)); it('renders mocked component with injected template properly', () => { // do not remove, it checks casts from MockDebugElement to DebugElement. diff --git a/tests-jasmine/internal-only-nested/fixtures.components.ts b/tests/internal-only-nested/fixtures.components.ts similarity index 100% rename from tests-jasmine/internal-only-nested/fixtures.components.ts rename to tests/internal-only-nested/fixtures.components.ts diff --git a/tests-jasmine/internal-only-nested/fixtures.modules.ts b/tests/internal-only-nested/fixtures.modules.ts similarity index 100% rename from tests-jasmine/internal-only-nested/fixtures.modules.ts rename to tests/internal-only-nested/fixtures.modules.ts diff --git a/tests-jest/internal-only-nested/test.spec.ts b/tests/internal-only-nested/test.spec.ts similarity index 88% rename from tests-jest/internal-only-nested/test.spec.ts rename to tests/internal-only-nested/test.spec.ts index 304eecd838..2a336f14e5 100644 --- a/tests-jest/internal-only-nested/test.spec.ts +++ b/tests/internal-only-nested/test.spec.ts @@ -5,12 +5,7 @@ import { InternalComponent } from './fixtures.components'; import { TargetModule } from './fixtures.modules'; describe('InternalOnlyNested:real', () => { - beforeEach(async done => { - await TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents(); - done(); - }); + beforeEach(() => MockBuilder(TargetModule)); it('should render', () => { expect(() => { diff --git a/tests-jasmine/internal-only/fixtures.components.ts b/tests/internal-only/fixtures.components.ts similarity index 100% rename from tests-jasmine/internal-only/fixtures.components.ts rename to tests/internal-only/fixtures.components.ts diff --git a/tests-jasmine/internal-only/fixtures.modules.ts b/tests/internal-only/fixtures.modules.ts similarity index 100% rename from tests-jasmine/internal-only/fixtures.modules.ts rename to tests/internal-only/fixtures.modules.ts diff --git a/tests-jest/internal-only/test.spec.ts b/tests/internal-only/test.spec.ts similarity index 87% rename from tests-jest/internal-only/test.spec.ts rename to tests/internal-only/test.spec.ts index 42d5e9332d..14db45b137 100644 --- a/tests-jest/internal-only/test.spec.ts +++ b/tests/internal-only/test.spec.ts @@ -5,12 +5,7 @@ import { InternalComponent } from './fixtures.components'; import { TargetModule } from './fixtures.modules'; describe('InternalOnly:real', () => { - beforeEach(async done => { - await TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents(); - done(); - }); + beforeEach(() => MockBuilder(TargetModule)); it('should render', () => { expect(() => { diff --git a/tests-jasmine/internal-vs-external/fixtures.components.ts b/tests/internal-vs-external/fixtures.components.ts similarity index 100% rename from tests-jasmine/internal-vs-external/fixtures.components.ts rename to tests/internal-vs-external/fixtures.components.ts diff --git a/tests-jasmine/internal-vs-external/fixtures.modules.ts b/tests/internal-vs-external/fixtures.modules.ts similarity index 100% rename from tests-jasmine/internal-vs-external/fixtures.modules.ts rename to tests/internal-vs-external/fixtures.modules.ts diff --git a/tests-jasmine/internal-vs-external/test.spec.ts b/tests/internal-vs-external/test.spec.ts similarity index 84% rename from tests-jasmine/internal-vs-external/test.spec.ts rename to tests/internal-vs-external/test.spec.ts index fc2a47279a..4dfa255af6 100644 --- a/tests-jasmine/internal-vs-external/test.spec.ts +++ b/tests/internal-vs-external/test.spec.ts @@ -1,16 +1,11 @@ import { TestBed } from '@angular/core/testing'; -import { MockBuilder, MockModule, MockRender } from 'ng-mocks'; +import { MockBuilder, MockRender } from 'ng-mocks'; import { ExternalComponent, InternalComponent } from './fixtures.components'; import { TargetModule } from './fixtures.modules'; describe('InternalVsExternal:real', () => { - beforeEach(async done => { - await TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents(); - done(); - }); + beforeEach(() => MockBuilder(TargetModule)); it('should render', () => { const fixture = MockRender(ExternalComponent); @@ -46,12 +41,7 @@ describe('InternalVsExternal:mock', () => { }); describe('InternalVsExternal:legacy', () => { - beforeEach(async done => { - await TestBed.configureTestingModule({ - imports: [MockModule(TargetModule)], - }).compileComponents(); - done(); - }); + beforeEach(() => MockBuilder().mock(TargetModule)); it('should render', () => { const fixture = MockRender(ExternalComponent); diff --git a/tests-jasmine/mock-builder-by-directive/fixtures.components.ts b/tests/mock-builder-by-directive/fixtures.components.ts similarity index 100% rename from tests-jasmine/mock-builder-by-directive/fixtures.components.ts rename to tests/mock-builder-by-directive/fixtures.components.ts diff --git a/tests-jasmine/mock-builder-by-directive/fixtures.modules.ts b/tests/mock-builder-by-directive/fixtures.modules.ts similarity index 100% rename from tests-jasmine/mock-builder-by-directive/fixtures.modules.ts rename to tests/mock-builder-by-directive/fixtures.modules.ts diff --git a/tests-jasmine/mock-builder-by-directive/test.spec.ts b/tests/mock-builder-by-directive/test.spec.ts similarity index 83% rename from tests-jasmine/mock-builder-by-directive/test.spec.ts rename to tests/mock-builder-by-directive/test.spec.ts index c7a23c0451..dcd7b6cf01 100644 --- a/tests-jasmine/mock-builder-by-directive/test.spec.ts +++ b/tests/mock-builder-by-directive/test.spec.ts @@ -1,16 +1,10 @@ -import { TestBed } from '@angular/core/testing'; import { MockBuilder, MockComponent, MockRender, ngMocks } from 'ng-mocks'; import { InternalComponent } from './fixtures.components'; import { TargetModule } from './fixtures.modules'; describe('MockBuilderByDirective:real', () => { - beforeEach(async done => { - await TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents(); - done(); - }); + beforeEach(() => MockBuilder(TargetModule)); it('should render', () => { const fixture = MockRender(InternalComponent); diff --git a/tests-jasmine/mock-builder-keeps-application-module/fixtures.components.ts b/tests/mock-builder-keeps-application-module/fixtures.components.ts similarity index 100% rename from tests-jasmine/mock-builder-keeps-application-module/fixtures.components.ts rename to tests/mock-builder-keeps-application-module/fixtures.components.ts diff --git a/tests-jasmine/mock-builder-keeps-application-module/fixtures.modules.ts b/tests/mock-builder-keeps-application-module/fixtures.modules.ts similarity index 100% rename from tests-jasmine/mock-builder-keeps-application-module/fixtures.modules.ts rename to tests/mock-builder-keeps-application-module/fixtures.modules.ts diff --git a/tests-jasmine/mock-builder-keeps-application-module/test.spec.ts b/tests/mock-builder-keeps-application-module/test.spec.ts similarity index 92% rename from tests-jasmine/mock-builder-keeps-application-module/test.spec.ts rename to tests/mock-builder-keeps-application-module/test.spec.ts index 2823fe5354..70cefa8af1 100644 --- a/tests-jasmine/mock-builder-keeps-application-module/test.spec.ts +++ b/tests/mock-builder-keeps-application-module/test.spec.ts @@ -6,11 +6,7 @@ import { TargetComponent } from './fixtures.components'; import { TARGET_TOKEN, TargetModule } from './fixtures.modules'; describe('MockBuilderKeepsApplicationModule:real', () => { - beforeEach(() => - TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents() - ); + beforeEach(() => MockBuilder(TargetModule)); it('should render', () => { const fixture = MockRender(TargetComponent); diff --git a/tests-jasmine/module-with-factory-tokens/fixtures.ts b/tests/module-with-factory-tokens/fixtures.ts similarity index 100% rename from tests-jasmine/module-with-factory-tokens/fixtures.ts rename to tests/module-with-factory-tokens/fixtures.ts diff --git a/tests-jasmine/module-with-factory-tokens/test.spec.ts b/tests/module-with-factory-tokens/test.spec.ts similarity index 100% rename from tests-jasmine/module-with-factory-tokens/test.spec.ts rename to tests/module-with-factory-tokens/test.spec.ts diff --git a/tests-jasmine/module-with-tokens/fixtures.ts b/tests/module-with-tokens/fixtures.ts similarity index 100% rename from tests-jasmine/module-with-tokens/fixtures.ts rename to tests/module-with-tokens/fixtures.ts diff --git a/tests-jasmine/module-with-tokens/test.spec.ts b/tests/module-with-tokens/test.spec.ts similarity index 100% rename from tests-jasmine/module-with-tokens/test.spec.ts rename to tests/module-with-tokens/test.spec.ts diff --git a/tests-jasmine/nested-before-each/fixtures.components.ts b/tests/nested-before-each/fixtures.components.ts similarity index 100% rename from tests-jasmine/nested-before-each/fixtures.components.ts rename to tests/nested-before-each/fixtures.components.ts diff --git a/tests-jasmine/nested-before-each/fixtures.modules.ts b/tests/nested-before-each/fixtures.modules.ts similarity index 100% rename from tests-jasmine/nested-before-each/fixtures.modules.ts rename to tests/nested-before-each/fixtures.modules.ts diff --git a/tests-jasmine/nested-before-each/test.spec.ts b/tests/nested-before-each/test.spec.ts similarity index 85% rename from tests-jasmine/nested-before-each/test.spec.ts rename to tests/nested-before-each/test.spec.ts index 1aa18a7b6a..0e911651bc 100644 --- a/tests-jasmine/nested-before-each/test.spec.ts +++ b/tests/nested-before-each/test.spec.ts @@ -1,6 +1,6 @@ import { Type } from '@angular/core'; import { TestBed } from '@angular/core/testing'; -import { MockComponent } from 'ng-mocks'; +import { MockBuilder, MockComponent } from 'ng-mocks'; import { InternalComponent } from './fixtures.components'; @@ -11,10 +11,7 @@ describe('nested-before-each', () => { beforeEach(() => { level = 0; mock = MockComponent(InternalComponent); - TestBed.configureTestingModule({ - declarations: [mock], - }); - return TestBed.compileComponents(); + return MockBuilder(mock); }); describe('tested', () => { @@ -48,10 +45,7 @@ describe('nested-before-all', () => { level = 0; mock = MockComponent(InternalComponent); TestBed.resetTestingModule(); - TestBed.configureTestingModule({ - declarations: [mock], - }); - return TestBed.compileComponents(); + return MockBuilder(mock); }); describe('tested', () => { diff --git a/tests-jasmine/normal-usage-after-mock-builder/fixtures.components.ts b/tests/normal-usage-after-mock-builder/fixtures.components.ts similarity index 100% rename from tests-jasmine/normal-usage-after-mock-builder/fixtures.components.ts rename to tests/normal-usage-after-mock-builder/fixtures.components.ts diff --git a/tests-jasmine/normal-usage-after-mock-builder/fixtures.modules.ts b/tests/normal-usage-after-mock-builder/fixtures.modules.ts similarity index 100% rename from tests-jasmine/normal-usage-after-mock-builder/fixtures.modules.ts rename to tests/normal-usage-after-mock-builder/fixtures.modules.ts diff --git a/tests-jasmine/normal-usage-after-mock-builder/fixtures.services.ts b/tests/normal-usage-after-mock-builder/fixtures.services.ts similarity index 100% rename from tests-jasmine/normal-usage-after-mock-builder/fixtures.services.ts rename to tests/normal-usage-after-mock-builder/fixtures.services.ts diff --git a/tests-jest/normal-usage-after-mock-builder/test.spec.ts b/tests/normal-usage-after-mock-builder/test.spec.ts similarity index 69% rename from tests-jest/normal-usage-after-mock-builder/test.spec.ts rename to tests/normal-usage-after-mock-builder/test.spec.ts index 5603426b8a..f921a909ca 100644 --- a/tests-jest/normal-usage-after-mock-builder/test.spec.ts +++ b/tests/normal-usage-after-mock-builder/test.spec.ts @@ -1,15 +1,10 @@ -import { TestBed } from '@angular/core/testing'; import { MockBuilder, MockRender } from 'ng-mocks'; import { FakeComponent, RealComponent, TargetComponent } from './fixtures.components'; import { TargetModule } from './fixtures.modules'; describe('normal-usage-after-mock-builder:real1', () => { - beforeEach(() => - TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents() - ); + beforeEach(() => MockBuilder(TargetModule)); it('renders real component because we did not use MockBuilder.replace yet', () => { const fixture = MockRender(TargetComponent); @@ -18,11 +13,7 @@ describe('normal-usage-after-mock-builder:real1', () => { }); describe('normal-usage-after-mock-builder:mock', () => { - beforeEach(() => - TestBed.configureTestingModule( - MockBuilder().keep(TargetModule).replace(RealComponent, FakeComponent, { dependency: true }).build() - ).compileComponents() - ); + beforeEach(() => MockBuilder().keep(TargetModule).replace(RealComponent, FakeComponent, { dependency: true })); it('renders fake component because we used MockBuilder.replace', () => { const fixture = MockRender(TargetComponent); @@ -31,11 +22,7 @@ describe('normal-usage-after-mock-builder:mock', () => { }); describe('normal-usage-after-mock-builder:real2', () => { - beforeEach(() => - TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents() - ); + beforeEach(() => MockBuilder(TargetModule)); it('has to render real component after MockBuilder.replace', () => { const fixture = MockRender(TargetComponent); diff --git a/tests/on-push/on-push.spec.ts b/tests/on-push/on-push.spec.ts new file mode 100644 index 0000000000..52cfa53542 --- /dev/null +++ b/tests/on-push/on-push.spec.ts @@ -0,0 +1,77 @@ +import { ChangeDetectionStrategy, Component, Input } from '@angular/core'; +import { MockBuilder, MockRender } from 'ng-mocks'; + +@Component({ + changeDetection: ChangeDetectionStrategy.OnPush, + selector: 'item-list', + template: '{{items.length}}', +}) +export class ItemListComponent { + @Input() items: string[]; +} + +describe('ChangeDetectionStrategy.OnPush:real', () => { + beforeEach(() => MockBuilder(ItemListComponent)); + + it('should show 0 if no items', () => { + const fixture = MockRender(ItemListComponent, { + items: [], + }); + expect(fixture.point.nativeElement.innerHTML).toEqual('0'); + }); + + it('should show 0 if items pushed to array but not changed reference', () => { + const params: { items: string[] } = { + items: [], + }; + const fixture = MockRender(ItemListComponent, params); + fixture.componentInstance.items.push('demo'); + fixture.detectChanges(); + + expect(fixture.point.nativeElement.innerHTML).toEqual('0'); + }); + + it('should show 1 if items array changed reference', () => { + const params: { items: string[] } = { + items: [], + }; + const fixture = MockRender(ItemListComponent, params); + fixture.componentInstance.items = ['demo']; + fixture.detectChanges(); + + expect(fixture.point.nativeElement.innerHTML).toEqual('1'); + }); +}); + +describe('ChangeDetectionStrategy.OnPush:mock', () => { + beforeEach(() => MockBuilder(ItemListComponent)); + + it('should show 0 if no items', () => { + const fixture = MockRender(ItemListComponent, { + items: [], + }); + expect(fixture.point.nativeElement.innerHTML).toEqual('0'); + }); + + it('should show 0 if items pushed to array but not changed reference', () => { + const params: { items: string[] } = { + items: [], + }; + const fixture = MockRender(ItemListComponent, params); + fixture.componentInstance.items.push('demo'); + fixture.detectChanges(); + + expect(fixture.point.nativeElement.innerHTML).toEqual('0'); + }); + + it('should show 1 if items array changed reference', () => { + const params: { items: string[] } = { + items: [], + }; + const fixture = MockRender(ItemListComponent, params); + fixture.componentInstance.items = ['demo']; + fixture.detectChanges(); + + expect(fixture.point.nativeElement.innerHTML).toEqual('1'); + }); +}); diff --git a/tests-jasmine/provider-with-dependency/fixtures.components.ts b/tests/provider-with-dependency/fixtures.components.ts similarity index 100% rename from tests-jasmine/provider-with-dependency/fixtures.components.ts rename to tests/provider-with-dependency/fixtures.components.ts diff --git a/tests-jasmine/provider-with-dependency/fixtures.modules.ts b/tests/provider-with-dependency/fixtures.modules.ts similarity index 100% rename from tests-jasmine/provider-with-dependency/fixtures.modules.ts rename to tests/provider-with-dependency/fixtures.modules.ts diff --git a/tests-jasmine/provider-with-dependency/fixtures.services.ts b/tests/provider-with-dependency/fixtures.services.ts similarity index 100% rename from tests-jasmine/provider-with-dependency/fixtures.services.ts rename to tests/provider-with-dependency/fixtures.services.ts diff --git a/tests-jasmine/provider-with-dependency/test.spec.ts b/tests/provider-with-dependency/test.spec.ts similarity index 100% rename from tests-jasmine/provider-with-dependency/test.spec.ts rename to tests/provider-with-dependency/test.spec.ts diff --git a/tests-jasmine/rerender-rendered-content-child/fixtures.components.ts b/tests/rerender-rendered-content-child/fixtures.components.ts similarity index 100% rename from tests-jasmine/rerender-rendered-content-child/fixtures.components.ts rename to tests/rerender-rendered-content-child/fixtures.components.ts diff --git a/tests-jasmine/rerender-rendered-content-child/fixtures.module.ts b/tests/rerender-rendered-content-child/fixtures.module.ts similarity index 100% rename from tests-jasmine/rerender-rendered-content-child/fixtures.module.ts rename to tests/rerender-rendered-content-child/fixtures.module.ts diff --git a/tests-jasmine/rerender-rendered-content-child/test.spec.ts b/tests/rerender-rendered-content-child/test.spec.ts similarity index 100% rename from tests-jasmine/rerender-rendered-content-child/test.spec.ts rename to tests/rerender-rendered-content-child/test.spec.ts diff --git a/tests-jasmine/shared-mocked-module/fixtures.components.ts b/tests/shared-mocked-module/fixtures.components.ts similarity index 100% rename from tests-jasmine/shared-mocked-module/fixtures.components.ts rename to tests/shared-mocked-module/fixtures.components.ts diff --git a/tests-jasmine/shared-mocked-module/fixtures.modules.ts b/tests/shared-mocked-module/fixtures.modules.ts similarity index 100% rename from tests-jasmine/shared-mocked-module/fixtures.modules.ts rename to tests/shared-mocked-module/fixtures.modules.ts diff --git a/tests-jasmine/shared-mocked-module/test.spec.ts b/tests/shared-mocked-module/test.spec.ts similarity index 88% rename from tests-jasmine/shared-mocked-module/test.spec.ts rename to tests/shared-mocked-module/test.spec.ts index 9aa9952194..d821d41194 100644 --- a/tests-jasmine/shared-mocked-module/test.spec.ts +++ b/tests/shared-mocked-module/test.spec.ts @@ -1,16 +1,10 @@ -import { TestBed } from '@angular/core/testing'; import { MockBuilder, MockComponent, MockRender, ngMocks } from 'ng-mocks'; import { MyComponent, TargetComponent } from './fixtures.components'; import { TargetModule } from './fixtures.modules'; describe('SharedMockedModule:real', () => { - beforeEach(async done => { - await TestBed.configureTestingModule({ - imports: [TargetModule], - }).compileComponents(); - done(); - }); + beforeEach(() => MockBuilder(TargetModule)); it('should render', () => { const fixture = MockRender(TargetComponent); diff --git a/tests-jasmine/spies/fixtures.components.ts b/tests/spies/fixtures.components.ts similarity index 100% rename from tests-jasmine/spies/fixtures.components.ts rename to tests/spies/fixtures.components.ts diff --git a/tests-jasmine/spies/fixtures.modules.ts b/tests/spies/fixtures.modules.ts similarity index 100% rename from tests-jasmine/spies/fixtures.modules.ts rename to tests/spies/fixtures.modules.ts diff --git a/tests-jasmine/spies/fixtures.providers.ts b/tests/spies/fixtures.providers.ts similarity index 100% rename from tests-jasmine/spies/fixtures.providers.ts rename to tests/spies/fixtures.providers.ts diff --git a/tests-jest/spies/test.spec.ts b/tests/spies/test.spec.ts similarity index 63% rename from tests-jest/spies/test.spec.ts rename to tests/spies/test.spec.ts index d4d89ae8a4..d5ddf33188 100644 --- a/tests-jest/spies/test.spec.ts +++ b/tests/spies/test.spec.ts @@ -1,17 +1,16 @@ -import { inject, TestBed } from '@angular/core/testing'; -import { MockModule, MockRender, MockService, ngMocks } from 'ng-mocks'; +import { inject } from '@angular/core/testing'; +import { MockBuilder, MockRender, MockService, ngMocks } from 'ng-mocks'; import { TargetComponent } from './fixtures.components'; import { TargetModule } from './fixtures.modules'; import { TargetService } from './fixtures.providers'; +// fix to support both jasmine and jest in the test +declare const jest: any; +declare const jasmine: any; + describe('spies:real', () => { - beforeEach(() => - TestBed.configureTestingModule({ - declarations: [TargetComponent], - imports: [TargetModule], - }).compileComponents() - ); + beforeEach(() => MockBuilder(TargetComponent).keep(TargetModule)); it('should render', () => { const fixture = MockRender(TargetComponent); @@ -23,19 +22,14 @@ describe('spies:real', () => { describe('spies:manual-mock', () => { beforeEach(() => { - const spy = MockService({}); - ngMocks.stub(spy, 'echo').mockReturnValue('fake'); - - return TestBed.configureTestingModule({ - declarations: [TargetComponent], - imports: [MockModule(TargetModule)], - providers: [ - { - provide: TargetService, - useValue: spy, - }, - ], - }).compileComponents(); + const spy = MockService(TargetService); + if (typeof jest !== 'undefined') { + ngMocks.stub(spy, 'echo').mockReturnValue('fake'); + } else if (typeof jasmine !== 'undefined') { + ngMocks.stub(spy, 'echo').and.returnValue('fake'); + } + + return MockBuilder(TargetComponent, TargetModule).mock(TargetService, spy); }); it('should get manually mocked service', inject([TargetService], (targetService: TargetService) => { @@ -50,12 +44,7 @@ describe('spies:manual-mock', () => { }); describe('spies:auto-mock', () => { - beforeEach(() => - TestBed.configureTestingModule({ - declarations: [TargetComponent], - imports: [MockModule(TargetModule)], - }).compileComponents() - ); + beforeEach(() => MockBuilder(TargetComponent, TargetModule)); it('should get already mocked service', inject([TargetService], (targetService: TargetService) => { const fixture = MockRender(TargetComponent); @@ -63,7 +52,11 @@ describe('spies:auto-mock', () => { expect(component).toBeDefined(); expect(targetService.echo).toHaveBeenCalledTimes(1); expect(targetService.echo).toHaveBeenCalledWith('constructor'); - ngMocks.stub(targetService, 'echo').mockReturnValue('faked'); + if (typeof jest !== 'undefined') { + ngMocks.stub(targetService, 'echo').mockReturnValue('faked'); + } else if (typeof jasmine !== 'undefined') { + ngMocks.stub(targetService, 'echo').and.returnValue('faked'); + } expect(component.echo()).toEqual('faked'); expect(targetService.echo).toHaveBeenCalledTimes(2); })); diff --git a/tests-jasmine/structural-directives/custom-ng-for-with-of.directive.ts b/tests/structural-directives/custom-ng-for-with-of.directive.ts similarity index 100% rename from tests-jasmine/structural-directives/custom-ng-for-with-of.directive.ts rename to tests/structural-directives/custom-ng-for-with-of.directive.ts diff --git a/tests-jasmine/structural-directives/custom-ng-for-without-of.directive.ts b/tests/structural-directives/custom-ng-for-without-of.directive.ts similarity index 100% rename from tests-jasmine/structural-directives/custom-ng-for-without-of.directive.ts rename to tests/structural-directives/custom-ng-for-without-of.directive.ts diff --git a/tests-jasmine/structural-directives/custom-ng-if.directive.ts b/tests/structural-directives/custom-ng-if.directive.ts similarity index 100% rename from tests-jasmine/structural-directives/custom-ng-if.directive.ts rename to tests/structural-directives/custom-ng-if.directive.ts diff --git a/tests-jest/structural-directives/structural-directives.spec.ts b/tests/structural-directives/structural-directives.spec.ts similarity index 94% rename from tests-jest/structural-directives/structural-directives.spec.ts rename to tests/structural-directives/structural-directives.spec.ts index 19e61e1aec..87d86b467c 100644 --- a/tests-jest/structural-directives/structural-directives.spec.ts +++ b/tests/structural-directives/structural-directives.spec.ts @@ -1,16 +1,13 @@ -import { TestBed } from '@angular/core/testing'; -import { MockDirective, MockedDirective, MockRender, ngMocks } from 'ng-mocks'; +import { MockBuilder, MockDirective, MockedDirective, MockRender, ngMocks } from 'ng-mocks'; import { CustomNgForWithOfDirective } from './custom-ng-for-with-of.directive'; import { CustomNgForWithoutOfDirective } from './custom-ng-for-without-of.directive'; import { CustomNgIfDirective } from './custom-ng-if.directive'; describe('structural-directive-as-ng-for:real', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [CustomNgForWithOfDirective, CustomNgForWithoutOfDirective, CustomNgIfDirective], - }); - }); + beforeEach(() => + MockBuilder().keep(CustomNgForWithOfDirective).keep(CustomNgForWithoutOfDirective).keep(CustomNgIfDirective) + ); it('renders customNgForWithOf properly', () => { // should iterate against 3 string. @@ -69,15 +66,9 @@ describe('structural-directive-as-ng-for:real', () => { }); describe('structural-directive-as-ng-for:mock', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [ - MockDirective(CustomNgIfDirective), - MockDirective(CustomNgForWithOfDirective), - MockDirective(CustomNgForWithoutOfDirective), - ], - }); - }); + beforeEach(() => + MockBuilder().mock(CustomNgIfDirective).mock(CustomNgForWithOfDirective).mock(CustomNgForWithoutOfDirective) + ); it('mocks customNgIf properly', () => { const fixture = MockRender(