Мой API возвращает объект, подобный этому:
let recipeResponse = { id: 6,
createdAt: '2020-01-22T04:16:07.197Z',
updatedAt: '2020-01-22T04:16:07.197Z',
name: 'Laudantium omnis harum',
slug: 'Laudantium omnis harum',
description: null,
notes: null,
imageUrls: null,
ingredients: [],
instructions: [],
prepSeconds: null,
cookSeconds: null,
yield: null,
rating: '5.00',
ratingCount: 1,
keywords: [],
draft: false,
filters: [ 2, 3 ],
userRating: null }
Я хочу сравнить его со следующим «фиктивным» объектом и для них равным (с условиями ниже).
let expected = { name: 'Laudantium omnis harum',
slug: 'Laudantium omnis harum',
store:
StoreEntity {
name: 'Miss Guy Kris',
website: 'http://Torp.org/',
subdomain: 'in',
customerId: '07eb2b36-c6a6-4aa5-9bac-bd1110737f7f',
planId: '4739d45b-e238-4d89-8d4a-34d5d5f6e7ec',
subscriptionId: '479b5f5c-f3b9-4822-83d2-c0fac3638875',
status: 'active',
customDomain: null,
logoUrl:
'https://s3.amazonaws.com/logo.png',
theme: null,
id: 1,
createdAt: 2020-01-22T04:16:07.057Z,
updatedAt: 2020-01-22T04:16:07.057Z,
gaId: '' },
creator:
AccountEntity {
name: 'Mr. Dennis Wiegand',
email: 'Heaney_Felipe@hotmail.com',
store:
StoreEntity {
name: 'Miss Guy Kris',
website: 'http://Torp.org/',
subdomain: 'in',
customerId: '07eb2b36-c6a6-4aa5-9bac-bd1110737f7f',
planId: '4739d45b-e238-4d89-8d4a-34d5d5f6e7ec',
subscriptionId: '479b5f5c-f3b9-4822-83d2-c0fac3638875',
status: 'active',
customDomain: null,
logoUrl:
'https://s3.amazonaws.com/logo.png',
theme: null,
id: 1,
createdAt: 2020-01-22T04:16:07.057Z,
updatedAt: 2020-01-22T04:16:07.057Z,
gaId: '' },
role: 'admin',
pictureUrl: null,
id: 1,
createdAt: 2020-01-22T04:16:07.077Z,
updatedAt: 2020-01-22T04:16:07.077Z },
ingredients: [],
instructions: [],
keywords: [],
draft: false,
filters:
[ { name: 'Ipsa est delectus',
slug: 'Ipsa est delectus',
store: [StoreEntity],
label: 'Difficulty',
id: 3,
createdAt: 2020-01-22T04:16:07.091Z,
updatedAt: 2020-01-22T04:16:07.091Z,
schemaType: 'recipeCategory' },
{ name: 'Vel omnis',
slug: 'Vel omnis',
store: [StoreEntity],
label: 'Products',
id: 2,
createdAt: 2020-01-22T04:16:07.087Z,
updatedAt: 2020-01-22T04:16:07.087Z,
schemaType: 'recipeCategory' } ],
description: null,
notes: null,
imageUrls: null,
prepSeconds: null,
cookSeconds: null,
yield: null,
id: 6,
createdAt: 2020-01-22T04:16:07.197Z,
updatedAt: 2020-01-22T04:16:07.197Z,
rating: '5.00',
ratingCount: 1 }
Условия:
recipeResponse
не должно иметь свойств, которых нет в expected
, за исключением userRating: null
Что должно произойти, чтобы сделать это сравнение равно:
- добавление
userRating: null
к expected
- изменение типа
expected.createdAt
и expected.updatedAt
с даты на строку. - удаление
expected.creator
, expected.store
- map
expected.filters
, чтобы иметь только id
( и порядок элементов не должен учитываться )
Я начал писать пользовательский с API-интерфейсом, похожим на этот, но кажется безумным:
expect(recipeResponse).toAdvancedObjectEqual(expected, {
type: 'full',
unordered: ['filters'],
omitRight: ['creator', 'store'],
transformRight: {
createdAt: (date: Date) => date.toISOString(),
updatedAt: (date: Date) => date.toISOString(),
filters: (f: object[]) => f.map(f => (f as any).id),
userRating: () => null,
},
})
Есть ли более простой способ сделать это?