1717 * under the License.
1818 */
1919
20+ import { ATTRIBUTE_SERVICE_KEY } from './attribute_service' ;
2021import { mockAttributeService } from './attribute_service_mock' ;
2122import { coreMock } from '../../../../core/public/mocks' ;
2223
23- interface DefaultTestAttributes {
24+ interface TestAttributes {
2425 title : string ;
2526 testAttr1 ?: string ;
2627 testAttr2 ?: { array : unknown [ ] ; testAttr3 : string } ;
2728}
2829
29- interface DefaultByValueInput {
30+ interface TestByValueInput {
3031 id : string ;
31- attributes : DefaultTestAttributes ;
32- }
33-
34- const customAttributesKey = 'reallyNeatAttributesKey' ;
35- interface CustomTestByValueInput {
36- id : string ;
37- [ customAttributesKey ] : DefaultTestAttributes ;
32+ [ ATTRIBUTE_SERVICE_KEY ] : TestAttributes ;
3833}
3934
4035describe ( 'attributeService' , ( ) => {
4136 const defaultTestType = 'defaultTestType' ;
42- let attributes : DefaultTestAttributes ;
43- let byValueInput : DefaultByValueInput ;
37+ let attributes : TestAttributes ;
38+ let byValueInput : TestByValueInput ;
4439 let byReferenceInput : { id : string ; savedObjectId : string } ;
4540
4641 beforeEach ( ( ) => {
@@ -60,12 +55,10 @@ describe('attributeService', () => {
6055 } ) ;
6156
6257 describe ( 'determining input type' , ( ) => {
63- const defaultAttributeService = mockAttributeService < DefaultTestAttributes > ( defaultTestType ) ;
64- const customAttributeService = mockAttributeService <
65- DefaultTestAttributes ,
66- typeof customAttributesKey ,
67- CustomTestByValueInput
68- > ( defaultTestType ) ;
58+ const defaultAttributeService = mockAttributeService < TestAttributes > ( defaultTestType ) ;
59+ const customAttributeService = mockAttributeService < TestAttributes , TestByValueInput > (
60+ defaultTestType
61+ ) ;
6962
7063 it ( 'can determine input type given default types' , ( ) => {
7164 expect (
@@ -85,7 +78,7 @@ describe('attributeService', () => {
8578 expect (
8679 customAttributeService . inputIsRefType ( {
8780 id : '456' ,
88- [ customAttributesKey ] : { title : 'wow I am by value' } ,
81+ [ ATTRIBUTE_SERVICE_KEY ] : { title : 'wow I am by value' } ,
8982 } )
9083 ) . toBeFalsy ( ) ;
9184 } ) ;
@@ -97,7 +90,7 @@ describe('attributeService', () => {
9790 core . savedObjects . client . get = jest . fn ( ) . mockResolvedValueOnce ( {
9891 attributes,
9992 } ) ;
100- const attributeService = mockAttributeService < DefaultTestAttributes > (
93+ const attributeService = mockAttributeService < TestAttributes > (
10194 defaultTestType ,
10295 undefined ,
10396 core
@@ -106,38 +99,22 @@ describe('attributeService', () => {
10699 } ) ;
107100
108101 it ( 'returns attributes when when given value type input' , async ( ) => {
109- const attributeService = mockAttributeService < DefaultTestAttributes > ( defaultTestType ) ;
102+ const attributeService = mockAttributeService < TestAttributes > ( defaultTestType ) ;
110103 expect ( await attributeService . unwrapAttributes ( byValueInput ) ) . toEqual ( attributes ) ;
111104 } ) ;
112105
113- it ( 'returns attributes with custom key when given value type input' , async ( ) => {
114- const customAttributeService = mockAttributeService <
115- DefaultTestAttributes ,
116- typeof customAttributesKey ,
117- CustomTestByValueInput
118- > ( defaultTestType , { attributesKey : customAttributesKey } ) ;
119- expect (
120- await customAttributeService . unwrapAttributes ( {
121- id : '456' ,
122- [ customAttributesKey ] : attributes ,
123- } )
124- ) . toEqual ( attributes ) ;
125- } ) ;
126-
127106 it ( 'runs attributes through a custom unwrap method' , async ( ) => {
128107 const core = coreMock . createStart ( ) ;
129108 core . savedObjects . client . get = jest . fn ( ) . mockResolvedValueOnce ( {
130109 attributes,
131110 } ) ;
132- const attributeService = mockAttributeService < DefaultTestAttributes > (
111+ const attributeService = mockAttributeService < TestAttributes > (
133112 defaultTestType ,
134113 {
135- customUnwrapMethod : ( savedObject ) => {
136- return {
137- ...savedObject . attributes ,
138- testAttr2 : { array : [ 1 , 2 , 3 , 4 , 5 ] , testAttr3 : 'kibanana' } ,
139- } ;
140- } ,
114+ customUnwrapMethod : ( savedObject ) => ( {
115+ ...savedObject . attributes ,
116+ testAttr2 : { array : [ 1 , 2 , 3 , 4 , 5 ] , testAttr3 : 'kibanana' } ,
117+ } ) ,
141118 } ,
142119 core
143120 ) ;
@@ -150,24 +127,13 @@ describe('attributeService', () => {
150127
151128 describe ( 'wrapping attributes' , ( ) => {
152129 it ( 'returns given attributes when use ref type is false' , async ( ) => {
153- const attributeService = mockAttributeService < DefaultTestAttributes > ( defaultTestType ) ;
130+ const attributeService = mockAttributeService < TestAttributes > ( defaultTestType ) ;
154131 expect ( await attributeService . wrapAttributes ( attributes , false ) ) . toEqual ( { attributes } ) ;
155132 } ) ;
156133
157- it ( 'returns given attributes with custom key when use ref type is false' , async ( ) => {
158- const attributeService = mockAttributeService <
159- DefaultTestAttributes ,
160- typeof customAttributesKey ,
161- CustomTestByValueInput
162- > ( defaultTestType , { attributesKey : customAttributesKey } ) ;
163- expect ( await attributeService . wrapAttributes ( attributes , false ) ) . toEqual ( {
164- [ customAttributesKey ] : attributes ,
165- } ) ;
166- } ) ;
167-
168134 it ( 'updates existing saved object with new attributes when given id' , async ( ) => {
169135 const core = coreMock . createStart ( ) ;
170- const attributeService = mockAttributeService < DefaultTestAttributes > (
136+ const attributeService = mockAttributeService < TestAttributes > (
171137 defaultTestType ,
172138 undefined ,
173139 core
@@ -187,7 +153,7 @@ describe('attributeService', () => {
187153 core . savedObjects . client . create = jest . fn ( ) . mockResolvedValueOnce ( {
188154 id : '678' ,
189155 } ) ;
190- const attributeService = mockAttributeService < DefaultTestAttributes > (
156+ const attributeService = mockAttributeService < TestAttributes > (
191157 defaultTestType ,
192158 undefined ,
193159 core
@@ -199,8 +165,8 @@ describe('attributeService', () => {
199165 } ) ;
200166
201167 it ( 'uses custom save method when given an id' , async ( ) => {
202- const customSaveMethod = jest . fn ( ) . mockReturnValue ( { id : '678 ' } ) ;
203- const attributeService = mockAttributeService < DefaultTestAttributes > ( defaultTestType , {
168+ const customSaveMethod = jest . fn ( ) . mockReturnValue ( { id : '123 ' } ) ;
169+ const attributeService = mockAttributeService < TestAttributes > ( defaultTestType , {
204170 customSaveMethod,
205171 } ) ;
206172 expect ( await attributeService . wrapAttributes ( attributes , true , byReferenceInput ) ) . toEqual (
@@ -215,13 +181,13 @@ describe('attributeService', () => {
215181
216182 it ( 'uses custom save method given no id' , async ( ) => {
217183 const customSaveMethod = jest . fn ( ) . mockReturnValue ( { id : '678' } ) ;
218- const attributeService = mockAttributeService < DefaultTestAttributes > ( defaultTestType , {
184+ const attributeService = mockAttributeService < TestAttributes > ( defaultTestType , {
219185 customSaveMethod,
220186 } ) ;
221187 expect ( await attributeService . wrapAttributes ( attributes , true ) ) . toEqual ( {
222188 savedObjectId : '678' ,
223189 } ) ;
224- expect ( customSaveMethod ) . toHaveBeenCalledWith ( defaultTestType , attributes ) ;
190+ expect ( customSaveMethod ) . toHaveBeenCalledWith ( defaultTestType , attributes , undefined ) ;
225191 } ) ;
226192 } ) ;
227193} ) ;
0 commit comments