refactor(ccdaservice): test and refactor count entities function (#6805)
[openemr.git] / ccdaservice / utils / count-entities / count-entities.spec.js
blobccd3abfdb509c77f3bae1ceb38f0f73ebf4d553a
1 const { countEntities } = require('./count-entities');
3 describe('countEntities', () => {
4     test.each([
5         { description: 'I only have one value!' },
6         { npi: '2.16.840.1.113883.4.6', other: true },
7         { code: '409073007', other: false },
8         { extension: '45665', other: 10 },
9         { id: '47', other: null },
10         { date: '2023-09-17', other: {} },
11         { use: 'work place', other: [] },
12         { type: 'primary home', other: () => true },
13     ])('should return 1 if the input represents a single entity', (input) => {
14         expect(countEntities(input)).toEqual(1);
15     });
17     test.each([
18         [[{ a: 1 }, { b: 2 }, { c: 3 }], 3],
19         [
20             {
21                 description: `I have multiple values inside, but none has an entity key!`,
22                 explode: `Why not?`,
23             },
24             2,
25         ],
26     ])(
27         'should return the number of enumerable keys in the object if the input does not represent a single entity',
28         (input, result) => {
29             expect(countEntities(input)).toEqual(result);
30         }
31     );
33     test.each([null, undefined, 10, true, 'OpenEMR', () => {}])(
34         'should return zero if the input is %p',
35         (input) => {
36             expect(countEntities(input)).toEqual(0);
37         }
38     );
40     it('should return zero if the input is an empty array', () => {
41         expect(countEntities([])).toEqual(0);
42     });
43 });