/
createDirectAnswer.ts
136 lines (130 loc) · 4.07 KB
/
createDirectAnswer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { createDirectAnswer } from '../../../src/transformers/searchservice/createDirectAnswer';
import { DirectAnswerType } from '../../../src/models/searchservice/response/directanswer/DirectAnswerType';
it('can create a FeaturedSnippetDirectAnswer', () => {
const apiFeaturedSnippetDirectAnswer = {
type: 'FEATURED_SNIPPET',
answer: {
value: 'Honolulu. Hawaii',
fieldType: 'multi_line_text',
snippet: {
matchedSubstrings: [{ offset: 31, length: 16 }],
value: '... senator from 1997 to 2004. Obama was born in Honolulu. Hawaii. After graduating ...'
}
},
relatedItem: {
verticalConfigId: 'wiki_bios',
data: {}
}
};
const actualDirectAnswer = createDirectAnswer(apiFeaturedSnippetDirectAnswer);
const expectedDirectAnswer = {
type: DirectAnswerType.FeaturedSnippet,
value: 'Honolulu. Hawaii',
relatedResult: {},
verticalKey: 'wiki_bios',
fieldType: 'multi_line_text',
snippet: {
matchedSubstrings: [{ offset: 31, length: 16 }],
value: '... senator from 1997 to 2004. Obama was born in Honolulu. Hawaii. After graduating ...'
}
};
expect(actualDirectAnswer).toMatchObject(expectedDirectAnswer);
});
it('can create a FeaturedSnippetDirectAnswer with an HTML snippet', () => {
const apiFeaturedSnippetDirectAnswer = {
type: 'FEATURED_SNIPPET',
answer: {
value: 'Honolulu. Hawaii',
fieldType: 'html',
snippet: {
matchedSubstrings: [],
value: '<p>senator from 1997 to 2004. Obama was born in Honolulu. Hawaii. After graduating</p>'
}
},
relatedItem: {
verticalConfigId: 'wiki_bios',
data: {}
}
};
const actualDirectAnswer = createDirectAnswer(apiFeaturedSnippetDirectAnswer);
const expectedDirectAnswer = {
type: DirectAnswerType.FeaturedSnippet,
value: 'Honolulu. Hawaii',
relatedResult: {},
verticalKey: 'wiki_bios',
fieldType: 'html',
snippet: {
matchedSubstrings: [],
value: '<p>senator from 1997 to 2004. Obama was born in Honolulu. Hawaii. After graduating</p>'
}
};
expect(actualDirectAnswer).toMatchObject(expectedDirectAnswer);
});
it('can handle unexpected fieldType for a FeaturedSnippetDirectAnswer', () => {
const apiFeaturedSnippetDirectAnswer = {
type: 'FEATURED_SNIPPET',
answer: {
value: 'Honolulu. Hawaii',
fieldType: 'phone',
snippet: {
matchedSubstrings: [{ offset: 31, length: 16 }],
value: '111 222 3333'
}
},
relatedItem: {
verticalConfigId: 'wiki_bios',
data: {}
}
};
expect(() => createDirectAnswer(apiFeaturedSnippetDirectAnswer))
.toThrow('Unexpected fieldType for featured snippet direct answer: phone');
});
it('can create a FieldValueDirectAnswer', () => {
const apiFieldValueDirectAnswer = {
type: 'FIELD_VALUE',
answer: {
value: '18888888888',
entityName: 'Barack Obama',
fieldName: 'Phone Number',
fieldApiName: 'mainPhone',
fieldType: 'phone'
},
relatedItem: {
verticalConfigId: 'people',
data: {}
}
};
const actualDirectAnswer = createDirectAnswer(apiFieldValueDirectAnswer);
const expectedDirectAnswer = {
type: DirectAnswerType.FieldValue,
value: '18888888888',
relatedResult: {},
verticalKey: 'people',
entityName: 'Barack Obama',
fieldName: 'Phone Number',
fieldApiName: 'mainPhone',
fieldType: 'phone'
};
expect(actualDirectAnswer).toMatchObject(expectedDirectAnswer);
});
it('can create a custom FieldValueDirectAnswer', () => {
const apiFieldValueDirectAnswer = {
type: 'FIELD_VALUE',
answer: {
value: {
color: 'yellow',
os: 'android'
},
entityName: 'Obama Phone',
fieldName: 'Phone Description',
fieldApiName: 'phoneDescription',
fieldType: 'c3350634.specialtype'
},
relatedItem: {
verticalConfigId: 'phone',
data: {}
}
};
const actualDirectAnswer = createDirectAnswer(apiFieldValueDirectAnswer);
expect(actualDirectAnswer).toMatchObject(expect.objectContaining({ fieldType: 'unknown' }));
});