Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix query regression and unit testing improvements #148

Merged
merged 1 commit into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@
"**/node_modules/**"
]
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"src/styles.scss"
],
"scripts": []
}
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
"ng": "npx ng",
"start": "npx ng serve",
"build": "npx ng build",
"test": "npx ng test",
"test": "npx ng test spectator",
"test:jest": "npx ng run spectator-app:test-jest",
"test:jest:watch": "npx ng run spectator-app:test-jest --watch",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you removed the watch option?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can run both test and test:jest in watch mode with --watch so it felt a bit unneeded. ;-)

"lint": "npx ng lint",
"e2e": "npx ng e2e",
"format": "npx prettier --write \"{projects,src}/**/*.ts\"",
Expand Down
14 changes: 0 additions & 14 deletions projects/spectator/src/lib/errors.ts

This file was deleted.

10 changes: 10 additions & 0 deletions projects/spectator/src/lib/globals.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/**
* @deprecated Deprecated in favour of Spectator query methods and Spectator custom matchers
*
* To be removed in v4
*/
export function query(selector: string, parent?) {
return (parent || document).querySelector(selector);
}

/**
* @deprecated Deprecated in favour of Spectator query methods and Spectator custom matchers
*
* To be removed in v4
*/
export function queryAll(selector: string, parent?) {
return (parent || document).querySelectorAll(selector);
}
35 changes: 8 additions & 27 deletions projects/spectator/src/lib/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { createMouseEvent } from './event-objects';
import { typeInElement } from './type-in-element';
import { patchElementFocus } from './element-focus';
import { Observable } from 'rxjs';
import { SpectatorDebugElementNotFoundError } from './errors';
import { SpyObject } from './mock';
import { DOMSelector } from './dom-selectors';
import { Token } from './token';
Expand Down Expand Up @@ -67,15 +66,7 @@ export class Spectator<C> {
query<R>(directive: Type<R>): R;
query<R>(directiveOrSelector: Type<any> | string, options: { read: Token<R> }): R;
query<R>(directiveOrSelector: Type<any> | DOMSelector | string, options: { read: Token<R> } = { read: undefined }): R {
try {
return _getChild<R>(this.debugElement)(directiveOrSelector, options);
} catch (err) {
if (err instanceof SpectatorDebugElementNotFoundError) {
return null;
} else {
throw err;
}
}
return _getChild<R>(this.debugElement)(directiveOrSelector, options);
}

queryAll<R extends Element[]>(selector: string | DOMSelector): R;
Expand Down Expand Up @@ -290,13 +281,7 @@ export class Spectator<C> {
*/
export function _getChild<R>(debugElementRoot: DebugElement) {
return function(directiveOrSelector: Type<R> | DOMSelector | string, options: { read } = { read: undefined }): R {
const [child] = _getChildren<R>(debugElementRoot)(directiveOrSelector, options);

if (!child) {
throw new SpectatorDebugElementNotFoundError(`Cannot find a debug element for ${directiveOrSelector}`);
}

return child;
return _getChildren<R>(debugElementRoot)(directiveOrSelector, options)[0] || null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should at least add console info so that users will know what is missing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that was before also not the case, right? Because the error was catched?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was throw error

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry, I see. I'll fix it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, well, it is still thrown right? Because it is not catched?

};
}

Expand All @@ -312,24 +297,20 @@ export function _getChildren<R>(debugElementRoot: DebugElement) {
return directiveOrSelector.execute(debugElementRoot.nativeElement) as any[];
}

let debugElement: DebugElement[];
let debugElements: DebugElement[];

if (typeof directiveOrSelector === 'string') {
debugElement = debugElementRoot.queryAll(By.css(directiveOrSelector));
debugElements = debugElementRoot.queryAll(By.css(directiveOrSelector));
} else {
debugElement = debugElementRoot.queryAll(By.directive(directiveOrSelector));
}

if (!debugElement) {
throw new SpectatorDebugElementNotFoundError(`Cannot find a debug element for ${directiveOrSelector}`);
debugElements = debugElementRoot.queryAll(By.directive(directiveOrSelector));
}

if (options.read) {
return debugElement.map(debug => debug.injector.get(options.read));
return debugElements.map(debug => debug.injector.get(options.read));
} else if (typeof directiveOrSelector === 'string') {
return debugElement.map(debug => debug.nativeElement);
return debugElements.map(debug => debug.nativeElement);
} else {
return debugElement.map(debug => debug.componentInstance);
return debugElements.map(debug => debug.componentInstance);
}
};
}
Expand Down
1 change: 1 addition & 0 deletions projects/spectator/src/lib/mock-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Directive, EventEmitter, Type } from '@angular/core';
* MockDirective({ selector: 'some-directive' });
* MockDirective({ selector: 'some-directive', inputs: ['some-input', 'some-other-input'] });
*
* @deprecated Deprecated in favour of the `ng-mocks` implementation of `MockDirective`. To be be removed in the next major version.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in v4 PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

*/
export function MockDirective(options: Directive & { identifier?: Type<any> }): Directive {
const metadata: Directive & { identifier?: Type<any> } = {
Expand Down
12 changes: 12 additions & 0 deletions src/app/zippy/zippy.component.jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ describe('ZippyComponent', () => {
componentProviders: [{ provide: QueryService, useValue: 'componentProviders' }]
});

it('should should have a zippy component', () => {
host = createHost(`<zippy title="Zippy title"></zippy>`);

expect('zippy').toExist();
expect(host.queryHost('zippy')).toExist();
expect(host.query('zippy')).not.toExist();

expect('.non-existing').not.toExist();
expect(host.queryHost('.non-existing')).not.toExist();
expect(host.query('.non-existing')).not.toExist();
});

it('should display the title', () => {
host = createHost(`<zippy title="Zippy title"></zippy>`);
expect(host.query('.zippy__title')).toHaveText('Zippy title');
Expand Down
12 changes: 12 additions & 0 deletions src/app/zippy/zippy.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ describe('ZippyComponent', () => {
componentProviders: [{ provide: QueryService, useValue: 'componentProviders' }]
});

it('should should have a zippy component', () => {
host = createHost(`<zippy title="Zippy title"></zippy>`);

expect('zippy').toExist();
expect(host.queryHost('zippy')).toExist();
expect(host.query('zippy')).not.toExist();

expect('.non-existing').not.toExist();
expect(host.queryHost('.non-existing')).not.toExist();
expect(host.query('.non-existing')).not.toExist();
});

it('should display the title', () => {
host = createHost(`<zippy title="Zippy title"></zippy>`);
expect(host.query('.zippy__title')).toHaveText('Zippy title');
Expand Down
2 changes: 1 addition & 1 deletion src/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
preset: 'jest-preset-angular',
globals: {
'ts-jest': {
tsConfigFile: 'src/tsconfig.spec.json'
tsConfig: 'src/tsconfig.jest.json'
}
},
roots: ['src'],
Expand Down
29 changes: 29 additions & 0 deletions src/tsconfig.jest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"module": "commonjs",
"baseUrl": "./",
"types": [
"jest",
"node"
],
"paths": {
"@netbasal/spectator": [
"../projects/spectator/src/lib"
],
"@netbasal/spectator/jest": [
"../projects/spectator/jest/src"
]
}
},
"files": [
"test.ts",
"polyfills.ts"
],
"include": [
"**/*.jest.ts",
"**/*.d.ts",
"../projects/spectator/src/lib/matchers-types.d.ts"
]
}
3 changes: 1 addition & 2 deletions src/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"module": "commonjs",
"baseUrl": "./",
"types": [
"jest",
"jasmine",
"node"
],
"paths": {
Expand All @@ -23,7 +23,6 @@
],
"include": [
"**/*.spec.ts",
"**/*.jest.ts",
"**/*.d.ts",
"../projects/spectator/src/lib/matchers-types.d.ts"
]
Expand Down