Skip to content

Commit

Permalink
test: contemporary features
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed May 10, 2022
1 parent 8601984 commit 017d860
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 37 deletions.
12 changes: 6 additions & 6 deletions docs/articles/guides/http-interceptor.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ describe('TestHttpInterceptor', () => {
});

it('triggers interceptor', () => {
const fixture = MockRender('');
const client: HttpClient =
fixture.debugElement.injector.get(HttpClient);
const httpMock: HttpTestingController =
fixture.debugElement.injector.get(HttpTestingController);
MockRender();

// Let's do a simply request.
// Let's extract the service and http controller for testing.
const client = ngMocks.findInstance(HttpClient);
const httpMock = ngMocks.findInstance(HttpTestingController);

// Let's do a simple request.
client.get('/target').subscribe();

// Now we can assert that a header has been added to the request.
Expand Down
9 changes: 4 additions & 5 deletions docs/articles/guides/http-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ describe('TestHttpRequest', () => {
});

it('sends a request', () => {
const fixture = MockRender('');
MockRender();

// Let's extract the service and http controller for testing.
const service: TargetService =
fixture.debugElement.injector.get(TargetService);
const httpMock: HttpTestingController =
fixture.debugElement.injector.get(HttpTestingController);
const service = ngMocks.findInstance(TargetService);
const httpMock = ngMocks.findInstance(HttpTestingController);

// A simple subscription to check what the service returns.
let actual: any;
Expand Down
2 changes: 1 addition & 1 deletion docs/articles/guides/ngonchanges.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('TestLifecycleHooks', () => {
const service: TargetService =
fixture.debugElement.injector.get(TargetService);

// By default nothing should be initialized.
// By default, nothing should be initialized.
expect(service.onChanges).toHaveBeenCalledTimes(0);

// Now let's render the component.
Expand Down
13 changes: 7 additions & 6 deletions examples/TestHttpInterceptor/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
MockBuilder,
MockRender,
NG_MOCKS_INTERCEPTORS,
ngMocks,
} from 'ng-mocks';

// An interceptor we want to test.
Expand Down Expand Up @@ -92,13 +93,13 @@ describe('TestHttpInterceptor', () => {
});

it('triggers interceptor', () => {
const fixture = MockRender('');
const client: HttpClient =
fixture.debugElement.injector.get(HttpClient);
const httpMock: HttpTestingController =
fixture.debugElement.injector.get(HttpTestingController);
MockRender();

// Let's do a simply request.
// Let's extract the service and http controller for testing.
const client = ngMocks.findInstance(HttpClient);
const httpMock = ngMocks.findInstance(HttpTestingController);

// Let's do a simple request.
client.get('/target').subscribe();

// Now we can assert that a header has been added to the request.
Expand Down
11 changes: 5 additions & 6 deletions examples/TestHttpRequest/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { Injectable, NgModule } from '@angular/core';
import { Observable } from 'rxjs';

import { MockBuilder, MockRender } from 'ng-mocks';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

// A service that does http requests.
@Injectable()
Expand Down Expand Up @@ -39,12 +39,11 @@ describe('TestHttpRequest', () => {
});

it('sends a request', () => {
const fixture = MockRender('');
MockRender();

// Let's extract the service and http controller for testing.
const service: TargetService =
fixture.debugElement.injector.get(TargetService);
const httpMock: HttpTestingController =
fixture.debugElement.injector.get(HttpTestingController);
const service = ngMocks.findInstance(TargetService);
const httpMock = ngMocks.findInstance(HttpTestingController);

// A simple subscription to check what the service returns.
let actual: any;
Expand Down
2 changes: 1 addition & 1 deletion examples/TestLifecycleHooks/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('TestLifecycleHooks', () => {
const service: TargetService =
fixture.debugElement.injector.get(TargetService);

// By default nothing should be initialized.
// By default, nothing should be initialized.
expect(service.onChanges).toHaveBeenCalledTimes(0);

// Now let's render the component.
Expand Down
4 changes: 2 additions & 2 deletions libs/ng-mocks/src/lib/mock-directive/mock-directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('MockDirective', () => {
const debugElement = fixture.debugElement.query(
By.directive(ExampleDirective),
);
const element = debugElement.injector.get(ExampleDirective);
const element = ngMocks.get(debugElement, ExampleDirective);
expect(element.something).toEqual('hi');
expect(element.exampleDirective).toEqual('bye');
});
Expand All @@ -154,7 +154,7 @@ describe('MockDirective', () => {
const debugElement = fixture.debugElement.query(
By.directive(ExampleDirective),
);
const element = debugElement.injector.get(ExampleDirective);
const element = ngMocks.get(debugElement, ExampleDirective);

element.someOutput.emit(true);
expect(component.emitted).toEqual(true);
Expand Down
2 changes: 1 addition & 1 deletion libs/ng-mocks/src/lib/mock-helper/mock-helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('MockHelper:getDirective', () => {
const debugElement = fixture.debugElement.query(
By.directive(ExampleDirective),
);
const element = debugElement.injector.get(ExampleDirective);
const element = ngMocks.get(debugElement, ExampleDirective);

// Using helper.
const elementFromHelper = ngMocks.get(
Expand Down
2 changes: 1 addition & 1 deletion tests-e2e/src/ngxs/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SetValue {
defaults: 'init',
})
@Injectable()
export class TestState {
class TestState {
@((Action as any)(SetValue))
setValue(ctx: StateContext<string>, { value }: SetValue) {
ctx.setState(value);
Expand Down
2 changes: 1 addition & 1 deletion tests/issue-2097/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ describe('issue-2097', () => {
});

it('fails on unknown tokens', () => {
MockRender('');
MockRender();
expect(() =>
ngMocks.findInstance(new InjectionToken('TOKEN')),
).toThrowError(
Expand Down
12 changes: 9 additions & 3 deletions tests/issue-736/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import {
OnInit,
} from '@angular/core';

import { getMockedNgDefOf, MockBuilder, MockRender } from 'ng-mocks';
import {
getMockedNgDefOf,
MockBuilder,
MockRender,
ngMocks,
} from 'ng-mocks';

@Component({
selector: 'modal',
Expand Down Expand Up @@ -55,8 +60,9 @@ describe('issue-736', () => {
const fixture = MockRender(TargetComponent, undefined, false);

// getting current instance of mock ComponentFactoryResolver
const componentFactoryResolver =
fixture.debugElement.injector.get(ComponentFactoryResolver);
const componentFactoryResolver = ngMocks.findInstance(
ComponentFactoryResolver,
);

// its spied resolveComponentFactory shouldn't be called
// the bug was that it is not a spy anymore.
Expand Down
8 changes: 4 additions & 4 deletions tests/provider-never-mock/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { APP_ID, InjectionToken, NgModule } from '@angular/core';

import { MockBuilder, MockRender } from 'ng-mocks';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

const MY_APP_ID = new InjectionToken('MY_APP_ID');

Expand All @@ -23,14 +23,14 @@ describe('provider-never-mock', () => {
beforeEach(() => MockBuilder(null, TargetModule));

it('does not mock APP_ID', () => {
const fixture = MockRender('');
MockRender();

// should stay as it is.
const appId = fixture.debugElement.injector.get(APP_ID);
const appId = ngMocks.findInstance(APP_ID);
expect(appId).not.toEqual('');

// should be an empty string due to mocking.
const myAppId = fixture.debugElement.injector.get(MY_APP_ID);
const myAppId = ngMocks.findInstance(MY_APP_ID);
expect(myAppId).toEqual('');
});
});

0 comments on commit 017d860

Please sign in to comment.