1 | import {
|
2 | extractData,
|
3 | getPathOrExtractPathFromObject,
|
4 | handleResponse,
|
5 | } from './getOptions';
|
6 |
|
7 | const name = 'Joe';
|
8 |
|
9 | describe('getForAutocomplete', () => {
|
10 | describe('"extractData"', () => {
|
11 | it('should return label-value pair by ID', () => {
|
12 | const out = extractData(
|
13 | { name, id: 1, foo: 'bar' },
|
14 | 'name',
|
15 | );
|
16 | expect(out).toMatchObject({
|
17 | label: name,
|
18 | value: 1,
|
19 | foo: 'bar',
|
20 | });
|
21 | });
|
22 |
|
23 | it('should return label-value pair by name', () => {
|
24 | const out = extractData({ name, foo: 'bar' }, 'name');
|
25 | expect(out).toMatchObject({
|
26 | label: name,
|
27 | value: name,
|
28 | });
|
29 | });
|
30 |
|
31 | it('should return label-value pair by string', () => {
|
32 | const out = extractData(name, 'name');
|
33 | expect(out).toMatchObject({
|
34 | label: name,
|
35 | value: name,
|
36 | });
|
37 | });
|
38 | });
|
39 |
|
40 | describe('"getPathOrExtractFromObject"', () => {
|
41 | it('should return extractData fn', () => {
|
42 | const out = getPathOrExtractPathFromObject(
|
43 | false,
|
44 | 'name',
|
45 | )({
|
46 | name,
|
47 | });
|
48 |
|
49 | expect(out).toMatchObject({
|
50 | value: name,
|
51 | label: name,
|
52 | });
|
53 | });
|
54 |
|
55 | it('should return path from object', () => {
|
56 | const out = getPathOrExtractPathFromObject(
|
57 | true,
|
58 | 'name',
|
59 | )({
|
60 | name,
|
61 | });
|
62 |
|
63 | expect(out).toMatch(name);
|
64 | });
|
65 | });
|
66 |
|
67 | describe('"handleResponse"', () => {
|
68 | it('should map data response to label-value pairs', () => {
|
69 | const out = handleResponse(
|
70 | 'orders',
|
71 | 'seq',
|
72 | )({
|
73 | data: {
|
74 | orders: [
|
75 | {
|
76 | seq: 1,
|
77 | },
|
78 | {
|
79 | seq: 2,
|
80 | },
|
81 | ],
|
82 | },
|
83 | });
|
84 |
|
85 | expect(out).toEqual([
|
86 | {
|
87 | value: 1,
|
88 | label: 1,
|
89 | seq: 1,
|
90 | },
|
91 | {
|
92 | value: 2,
|
93 | label: 2,
|
94 | seq: 2,
|
95 | },
|
96 | ]);
|
97 | });
|
98 |
|
99 | it('should flatten response to array of strings', () => {
|
100 | const out = handleResponse(
|
101 | 'orders',
|
102 | 'seq',
|
103 | true,
|
104 | )({
|
105 | data: {
|
106 | orders: [
|
107 | {
|
108 | seq: 1,
|
109 | },
|
110 | {
|
111 | seq: 2,
|
112 | },
|
113 | ],
|
114 | },
|
115 | });
|
116 |
|
117 | expect(out).toEqual([1, 2]);
|
118 | });
|
119 | });
|
120 | });
|