Skip to content

Commit bc49e77

Browse files
[DSC-1932] Added unit tests case for 'dsSort' pipe
1 parent 1e25ca5 commit bc49e77

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { SortPipe } from './sort.pipe';
2+
3+
describe('SortPipe', () => {
4+
let pipe: SortPipe;
5+
6+
beforeEach(() => {
7+
pipe = new SortPipe();
8+
});
9+
10+
it('should return the original value if value is null or undefined', () => {
11+
expect(pipe.transform(null)).toBeNull();
12+
expect(pipe.transform(undefined)).toBeUndefined();
13+
});
14+
15+
it('should return the original value if no order is invalid', () => {
16+
const inputArray = [3, 1, 2];
17+
expect(pipe.transform(inputArray, '', null)).toEqual(inputArray);
18+
});
19+
20+
it('should sort the array in ascending order if no column is provided', () => {
21+
const inputArray = [3, 1, 2];
22+
const sortedArray = [1, 2, 3];
23+
expect(pipe.transform(inputArray, '')).toEqual(sortedArray);
24+
});
25+
26+
it('should sort the array in descending order if no column is provided and order is desc', () => {
27+
const inputArray = [3, 1, 2];
28+
const sortedArray = [3, 2, 1];
29+
expect(pipe.transform(inputArray, '', 'desc')).toEqual(sortedArray);
30+
});
31+
32+
it('should return the original array if it contains one or fewer elements', () => {
33+
const inputArray = [1];
34+
expect(pipe.transform(inputArray)).toEqual(inputArray);
35+
expect(pipe.transform([])).toEqual([]);
36+
});
37+
38+
it('should sort the array by a specific column in ascending order', () => {
39+
const inputArray = [
40+
{ label: 'banana' },
41+
{ label: 'apple' },
42+
{ label: 'cherry' }
43+
];
44+
const sortedArray = [
45+
{ label: 'apple' },
46+
{ label: 'banana' },
47+
{ label: 'cherry' }
48+
];
49+
expect(pipe.transform(inputArray, 'label')).toEqual(sortedArray);
50+
});
51+
52+
it('should sort the array by a specific column in descending order', () => {
53+
const inputArray = [
54+
{ label: 'banana' },
55+
{ label: 'apple' },
56+
{ label: 'cherry' }
57+
];
58+
const sortedArray = [
59+
{ label: 'cherry' },
60+
{ label: 'banana' },
61+
{ label: 'apple' }
62+
];
63+
expect(pipe.transform(inputArray, 'label', 'desc')).toEqual(sortedArray);
64+
});
65+
});

0 commit comments

Comments
 (0)