Skip to content

Commit 99736fc

Browse files
committed
feat(tests): add missing unit tests to have 100% test coverage
1 parent 644e1dc commit 99736fc

File tree

4 files changed

+48
-19
lines changed

4 files changed

+48
-19
lines changed

src/app/modules/angular-slickgrid/filters/__tests__/autoCompleteFilter.spec.ts

+32-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Filters } from '..';
44
import { AutoCompleteFilter } from '../autoCompleteFilter';
55
import { AutocompleteOption, Column, FieldType, FilterArguments, GridOption, OperatorType } from '../../models';
66
import { CollectionService } from './../../services/collection.service';
7-
import { of } from 'rxjs';
7+
import { of, Subject } from 'rxjs';
88

99
const containerId = 'demo-container';
1010

@@ -109,14 +109,12 @@ describe('AutoCompleteFilter', () => {
109109
}
110110
});
111111

112-
xit('should throw an error when "collectionAsync" Observable does not return a valid array', (done) => {
113-
try {
114-
mockColumn.filter.collectionAsync = of({ hello: 'world' });
115-
filter.init(filterArguments);
116-
} catch (e) {
112+
it('should throw an error when "collectionAsync" Observable does not return a valid array', (done) => {
113+
mockColumn.filter.collectionAsync = of({ hello: 'world' });
114+
filter.init(filterArguments).catch((e) => {
117115
expect(e.toString()).toContain(`Something went wrong while trying to pull the collection from the "collectionAsync" call in the AutoComplete Filter, the collection is not a valid array.`);
118116
done();
119-
}
117+
});
120118
});
121119

122120
it('should initialize the filter', () => {
@@ -299,6 +297,33 @@ describe('AutoCompleteFilter', () => {
299297
});
300298
});
301299

300+
it('should create the multi-select filter with a "collectionAsync" as an Observable and be able to call next on it', (done) => {
301+
const mockCollection = ['male', 'female'];
302+
mockColumn.filter.collectionAsync = of(mockCollection);
303+
304+
filterArguments.searchTerms = ['female'];
305+
filter.init(filterArguments);
306+
307+
setTimeout(() => {
308+
const filterElm = divContainer.querySelector<HTMLInputElement>('input.filter-gender');
309+
filter.setValues('male');
310+
311+
filterElm.focus();
312+
filterElm.dispatchEvent(new (window.window as any).KeyboardEvent('keyup', { keyCode: 97, bubbles: true, cancelable: true }));
313+
314+
// after await (or timeout delay) we'll get the Subject Observable
315+
mockCollection.push('other');
316+
(mockColumn.filter.collectionAsync as Subject<any[]>).next(mockCollection);
317+
318+
const autocompleteUlElms = document.body.querySelectorAll<HTMLUListElement>('ul.ui-autocomplete');
319+
const filterFilledElms = divContainer.querySelectorAll<HTMLInputElement>('input.filter-gender.filled');
320+
321+
expect(autocompleteUlElms.length).toBe(1);
322+
expect(filterFilledElms.length).toBe(1);
323+
done();
324+
});
325+
});
326+
302327
it('should create the filter and filter the string collection when "collectionFilterBy" is set', () => {
303328
mockColumn.filter = {
304329
collection: ['other', 'male', 'female'],

src/app/modules/angular-slickgrid/filters/__tests__/selectFilter.spec.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -539,14 +539,12 @@ describe('SelectFilter', () => {
539539
});
540540
});
541541

542-
xit('should throw an error when "collectionAsync" Observable does not return a valid array', (done) => {
543-
try {
544-
mockColumn.filter.collectionAsync = of({ hello: 'world' });
545-
filter.init(filterArguments, true);
546-
} catch (e) {
542+
it('should throw an error when "collectionAsync" Observable does not return a valid array', (done) => {
543+
mockColumn.filter.collectionAsync = of({ hello: 'world' });
544+
filter.init(filterArguments, true).catch((e) => {
547545
expect(e.toString()).toContain(`Something went wrong while trying to pull the collection from the "collectionAsync" call in the Select Filter, the collection is not a valid array.`);
548546
done();
549-
}
547+
});
550548
});
551549

552550
it('should create the multi-select filter with a default search term and have the HTML rendered when "enableRenderHtml" is set', () => {

src/app/modules/angular-slickgrid/filters/autoCompleteFilter.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class AutoCompleteFilter implements Filter {
107107
/**
108108
* Initialize the filter template
109109
*/
110-
init(args: FilterArguments) {
110+
init(args: FilterArguments): Promise<boolean> {
111111
if (!args) {
112112
throw new Error('[Angular-SlickGrid] A filter must always have an "init()" with valid arguments.');
113113
}
@@ -134,7 +134,9 @@ export class AutoCompleteFilter implements Filter {
134134
// if "collectionAsync" is already set by the user, it will resolve it first then after it will replace it with a Subject
135135
const collectionAsync = this.columnFilter && this.columnFilter.collectionAsync;
136136
if (collectionAsync) {
137-
this.renderOptionsAsync(collectionAsync); // create Subject after resolve (createCollectionAsyncSubject)
137+
return this.renderOptionsAsync(collectionAsync); // create Subject after resolve (createCollectionAsyncSubject)
138+
} else {
139+
return new Promise((resolve) => resolve(true));
138140
}
139141
}
140142

@@ -209,7 +211,7 @@ export class AutoCompleteFilter implements Filter {
209211
return outputCollection;
210212
}
211213

212-
protected async renderOptionsAsync(collectionAsync: Promise<any> | Observable<any> | Subject<any>) {
214+
protected async renderOptionsAsync(collectionAsync: Promise<any> | Observable<any> | Subject<any>): Promise<boolean> {
213215
let awaitedCollection: any = [];
214216

215217
if (collectionAsync) {
@@ -221,6 +223,7 @@ export class AutoCompleteFilter implements Filter {
221223
// doing this provide the user a way to call a "collectionAsync.next()"
222224
this.createCollectionAsyncSubject();
223225
}
226+
return true;
224227
}
225228

226229
/** Create or recreate an Observable Subject and reassign it to the "collectionAsync" object so user can call a "collectionAsync.next()" on it */

src/app/modules/angular-slickgrid/filters/selectFilter.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class SelectFilter implements Filter {
107107
/**
108108
* Initialize the filter template
109109
*/
110-
init(args: FilterArguments, isFilterFirstRender: boolean) {
110+
init(args: FilterArguments, isFilterFirstRender: boolean): Promise<boolean> {
111111
if (!args) {
112112
throw new Error('[Angular-SlickGrid] A filter must always have an "init()" with valid arguments.');
113113
}
@@ -155,7 +155,9 @@ export class SelectFilter implements Filter {
155155
// if "collectionAsync" is already set by the user, it will resolve it first then after it will replace it with a Subject
156156
const collectionAsync = this.columnFilter && this.columnFilter.collectionAsync;
157157
if (collectionAsync) {
158-
this.renderOptionsAsync(collectionAsync); // create Subject after resolve (createCollectionAsyncSubject)
158+
return this.renderOptionsAsync(collectionAsync); // create Subject after resolve (createCollectionAsyncSubject)
159+
} else {
160+
return new Promise((resolve) => resolve(true));
159161
}
160162
}
161163

@@ -252,7 +254,7 @@ export class SelectFilter implements Filter {
252254
return outputCollection;
253255
}
254256

255-
protected async renderOptionsAsync(collectionAsync: Promise<any> | Observable<any> | Subject<any>) {
257+
protected async renderOptionsAsync(collectionAsync: Promise<any> | Observable<any> | Subject<any>): Promise<boolean> {
256258
let awaitedCollection: any = [];
257259

258260
if (collectionAsync) {
@@ -264,6 +266,7 @@ export class SelectFilter implements Filter {
264266
// doing this provide the user a way to call a "collectionAsync.next()"
265267
this.createCollectionAsyncSubject();
266268
}
269+
return true;
267270
}
268271

269272
/** Create or recreate an Observable Subject and reassign it to the "collectionAsync" object so user can call a "collectionAsync.next()" on it */

0 commit comments

Comments
 (0)