Skip to content

Commit

Permalink
fix(ngMocks.findInstance): works without fixture help-me-mom#2311
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Apr 24, 2022
1 parent f458f69 commit 7752914
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,40 @@ import funcGetFromNode from '../func.get-from-node';
import funcGetLastFixture from '../func.get-last-fixture';
import funcParseFindArgs from '../func.parse-find-args';
import funcParseFindArgsName from '../func.parse-find-args-name';
import { getInjection } from '../../common/core.helpers';
import { Type } from '../../common/core.types';

import funcIsValidFindInstanceSelector from './func.is-valid-find-instance-selector';

const defaultNotFoundValue = {}; // simulating Symbol

export default (...args: any[]) => {
export default <T>(...args: any[]): T => {
const [el, sel, notFoundValue] = funcParseFindArgs(args, funcIsValidFindInstanceSelector, defaultNotFoundValue);
if (typeof sel !== 'function' && !isNgDef(sel, 't')) {
throw new Error('Only classes or tokens are accepted');
}

const declaration = getSourceOfMock(sel);
const result: any[] = [];
mockHelperCrawl(
mockHelperFind(funcGetLastFixture(), el, undefined),
node => {
funcGetFromNode(result, node, declaration);
const declaration: Type<T> = getSourceOfMock(sel);
const result: T[] = [];
const fixture = funcGetLastFixture();
if (fixture) {
mockHelperCrawl(
mockHelperFind(fixture, el, undefined),
node => {
funcGetFromNode(result, node, declaration);

return result.length > 0;
},
true,
);
} else {
try {
result.push(getInjection(declaration));
} catch {
// nothing to do
}
}

return result.length > 0;
},
true,
);
if (result.length > 0) {
return result[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import mockHelperFindAll from '../find/mock-helper.find-all';
import funcGetFromNode from '../func.get-from-node';
import funcGetLastFixture from '../func.get-last-fixture';
import funcParseFindArgs from '../func.parse-find-args';
import { getInjection } from '../../common/core.helpers';
import { Type } from '../../common/core.types';

import funcIsValidFindInstanceSelector from './func.is-valid-find-instance-selector';

Expand All @@ -14,21 +16,30 @@ export default <T>(...args: any[]): T[] => {
throw new Error('Only classes or tokens are accepted');
}

const declaration = getSourceOfMock(sel);
const declaration: Type<T> = getSourceOfMock(sel);
const result: T[] = [];
const scanned: any[] = [];
const elements = mockHelperFindAll(funcGetLastFixture(), el, undefined);
for (const element of elements) {
mockHelperCrawl(
element,
node => {
if (scanned.indexOf(node) === -1) {
funcGetFromNode(result, node, declaration);
scanned.push(node);
}
},
true,
);
const fixture = funcGetLastFixture();
if (fixture) {
const elements = mockHelperFindAll(fixture, el, undefined);
for (const element of elements) {
mockHelperCrawl(
element,
node => {
if (scanned.indexOf(node) === -1) {
funcGetFromNode(result, node, declaration);
scanned.push(node);
}
},
true,
);
}
} else {
try {
result.push(getInjection(declaration));
} catch {
// nothing to do
}
}

return result;
Expand Down
50 changes: 50 additions & 0 deletions tests/issue-2311/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Injectable, NgModule } from '@angular/core';

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

@Injectable()
class TargetService {
echo() {
return this.constructor.name;
}
}

@Injectable()
class FakeService {
echo() {
return this.constructor.name;
}
}

@NgModule({
providers: [TargetService],
})
class TargetModule {}

describe('issue-2311', () => {
beforeEach(() => MockBuilder(TargetService, TargetModule));

it('finds instance', () => {
const instance = ngMocks.findInstance(TargetService);
expect(instance.echo()).toEqual('TargetService');
});

it('finds instances', () => {
const instances = ngMocks.findInstances(TargetService);
const [instance] = instances;
expect(instances.length).toEqual(1);
expect(instance.echo()).toEqual('TargetService');
});

it('fails to find instance', () => {
const instance = ngMocks.findInstance(FakeService, undefined);
expect(instance).toBeUndefined();
});

it('fails to find instances', () => {
const instances = ngMocks.findInstances(FakeService);
const [instance] = instances;
expect(instances.length).toEqual(0);
expect(instance).toBeUndefined();
});
});

0 comments on commit 7752914

Please sign in to comment.