Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 54f8dc7

Browse files
authored
Merge pull request #182 from ghiscoding/feat/formatter-tests
feat(tests): add missing Formatter unit tests
2 parents ef32b1e + 2b67a61 commit 54f8dc7

File tree

5 files changed

+139
-6
lines changed

5 files changed

+139
-6
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { Column } from '../models';
2+
import { collectionEditorFormatter } from './collectionEditorFormatter';
3+
import { Editors } from '../editors';
4+
5+
describe('the CollectionEditor Formatter', () => {
6+
let columnDef: Column;
7+
8+
beforeEach(() => {
9+
columnDef = {
10+
editor: {
11+
model: Editors.singleSelect,
12+
collection: [{ value: 1, label: 'foo' }, { value: 2, label: 'bar' }]
13+
}
14+
} as Column;
15+
columnDef.internalColumnEditor = columnDef.editor;
16+
});
17+
18+
it('should return same output when no value is passed', () => {
19+
const valueArray = null;
20+
const result = collectionEditorFormatter(0, 0, valueArray, {} as Column, {});
21+
expect(result).toBe(null);
22+
});
23+
24+
it('should return an empty array when value passed is an empty array', () => {
25+
const valueArray = [];
26+
const result = collectionEditorFormatter(0, 0, valueArray, {} as Column, {});
27+
expect(result).toEqual([]);
28+
});
29+
30+
it('should return original value when input is not an array', () => {
31+
const inputValue = 'anything';
32+
const result = collectionEditorFormatter(0, 0, inputValue, {} as Column, {});
33+
expect(result).toBe(inputValue);
34+
});
35+
36+
it('should return a CSV string when provided collection is an array of objects', () => {
37+
const valueArray = [1, 2];
38+
const result = collectionEditorFormatter(0, 0, valueArray, columnDef, {});
39+
const outputCsv = 'foo, bar';
40+
expect(result).toBe(`<span title="${outputCsv}">${outputCsv}</span>`);
41+
});
42+
43+
it('should return a CSV string when provided collection is an array of strings', () => {
44+
const valueArray = ['foo', 'bar'];
45+
columnDef.editor.collection = ['foo', 'bar', 'apple'];
46+
47+
const result = collectionEditorFormatter(0, 0, valueArray, columnDef, {});
48+
49+
const outputCsv = 'foo, bar';
50+
expect(result).toBe(`<span title="${outputCsv}">${outputCsv}</span>`);
51+
});
52+
53+
it('should return a CSV string when provided collection is an array of objects', () => {
54+
const valueArray = [1, 2];
55+
columnDef.editor.collection = [{ id: 1, name: 'John' }, { id: 2, name: 'Bob' }];
56+
columnDef.editor.customStructure = { label: 'name', value: 'id' };
57+
58+
const result = collectionEditorFormatter(0, 0, valueArray, columnDef, {});
59+
60+
const outputCsv = 'John, Bob';
61+
expect(result).toBe(`<span title="${outputCsv}">${outputCsv}</span>`);
62+
});
63+
64+
it('should return a string when provided input value is an object', () => {
65+
const inputValue = 2;
66+
const result = collectionEditorFormatter(0, 0, inputValue, columnDef, {});
67+
expect(result).toBe('bar');
68+
});
69+
70+
it('should return an empty string when provided input value is an object that is not part of the collection', () => {
71+
const inputValue = 4;
72+
const result = collectionEditorFormatter(0, 0, inputValue, columnDef, {});
73+
expect(result).toBe('');
74+
});
75+
});

src/app/modules/angular-slickgrid/formatters/collectionEditorFormatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { findOrDefault } from '../services/utilities';
88
export const collectionEditorFormatter: Formatter = (row: number, cell: number, value: any, columnDef: Column, dataContext: any) => {
99
if (!value || !columnDef || !columnDef.internalColumnEditor || !columnDef.internalColumnEditor.collection
1010
|| !columnDef.internalColumnEditor.collection.length) {
11-
return '';
11+
return value;
1212
}
1313

1414
const { internalColumnEditor, internalColumnEditor: { collection } } = columnDef;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Column } from '../models';
2+
import { collectionFormatter } from './collectionFormatter';
3+
4+
describe('the Collection Formatter', () => {
5+
it('should return same output when no value is passed', () => {
6+
const valueArray = null;
7+
const result = collectionFormatter(0, 0, valueArray, {} as Column, {});
8+
expect(result).toBe(null);
9+
});
10+
11+
it('should return an empty array when value passed is an empty array', () => {
12+
const valueArray = [];
13+
const result = collectionFormatter(0, 0, valueArray, {} as Column, {});
14+
expect(result).toEqual([]);
15+
});
16+
17+
it('should return original value when input is not an array', () => {
18+
const inputValue = 'anything';
19+
const result = collectionFormatter(0, 0, inputValue, {} as Column, {});
20+
expect(result).toBe(inputValue);
21+
});
22+
23+
it('should return a CSV string when value passed is an array of objects', () => {
24+
const valueArray = [1, 2];
25+
const columnDef = { params: { collection: [{ value: 1, label: 'foo' }, { value: 2, label: 'bar' }] } } as Column;
26+
const result = collectionFormatter(0, 0, valueArray, columnDef, {});
27+
const outputCsv = 'foo, bar';
28+
expect(result).toBe(`<span title="${outputCsv}">${outputCsv}</span>`);
29+
});
30+
31+
it('should return a CSV string when value passed is an array of objects', () => {
32+
const valueArray = [1, 2];
33+
const columnDef = {
34+
params: {
35+
collection: [{ id: 1, name: 'John' }, { id: 2, name: 'Bob' }],
36+
customStructure: { label: 'name', value: 'id' }
37+
}
38+
} as Column;
39+
const result = collectionFormatter(0, 0, valueArray, columnDef, {});
40+
const outputCsv = 'John, Bob';
41+
expect(result).toBe(`<span title="${outputCsv}">${outputCsv}</span>`);
42+
});
43+
44+
it('should return a string when value passed is an object', () => {
45+
const inputValue = 2;
46+
const columnDef = { params: { collection: [{ value: 1, label: 'foo' }, { value: 2, label: 'bar' }] } } as Column;
47+
const result = collectionFormatter(0, 0, inputValue, columnDef, {});
48+
expect(result).toBe('bar');
49+
});
50+
51+
it('should return an empty string when value passed is an object that is not part of the collection', () => {
52+
const inputValue = 4;
53+
const columnDef = { params: { collection: [{ value: 1, label: 'foo' }, { value: 2, label: 'bar' }] } } as Column;
54+
const result = collectionFormatter(0, 0, inputValue, columnDef, {});
55+
expect(result).toBe('');
56+
});
57+
});

src/app/modules/angular-slickgrid/formatters/collectionFormatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { findOrDefault } from '../services/utilities';
88
export const collectionFormatter: Formatter = (row: number, cell: number, value: any, columnDef: Column, dataContext: any) => {
99
if (!value || !columnDef || !columnDef.params || !columnDef.params.collection
1010
|| !columnDef.params.collection.length) {
11-
return '';
11+
return value;
1212
}
1313

1414
const { params, params: { collection } } = columnDef;

src/app/modules/angular-slickgrid/formatters/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,17 @@ export const Formatters = {
7373
* @example
7474
* // the grid will display 'foo' and 'bar' and not 1 and 2 from your dataset
7575
* { params: { collection: [{ value: 1, label: 'foo'}, {value: 2, label: 'bar' }] }}
76-
* const dataset = [{ value: 1 },{ value: 2 }];
76+
* const dataset = [1, 2];
7777
*/
7878
collection: collectionFormatter,
7979

8080
/**
81-
* Looks up values from the columnDefinition.editor.collection property and displays the label in CSV or string format
81+
* Roughly the same as the "collectionFormatter" except that it
82+
* looks up values from the columnDefinition.editor.collection (instead of params) property and displays the label in CSV or string format
8283
* @example
8384
* // the grid will display 'foo' and 'bar' and not 1 and 2 from your dataset
84-
* { params: { collection: [{ value: 1, label: 'foo'}, {value: 2, label: 'bar' }] }}
85-
* const dataset = [{ value: 1 },{ value: 2 }];
85+
* { editor: { collection: [{ value: 1, label: 'foo'}, {value: 2, label: 'bar' }] }}
86+
* const dataset = [1, 2];
8687
*/
8788
collectionEditor: collectionEditorFormatter,
8889

0 commit comments

Comments
 (0)