我正在构建一个现场音乐演出列表应用程序在react 原住民与expo .
从Firebase获取GIG对象的数组,其形状如下:
gigs:
[
{
dateAndTime:{seconds:2345234748},
gigName: 'gigAtVenue',
...
},
{...}
]
在我的组件listByDay.txs
中,我编写了一些代码来过滤这个gig对象数组,并只返回当天的gig:
const gigsToday = gigs.filter((gig) => {
const formattedDate = format(new Date(currentDateMs), 'do MMMM Y')
const formattedGigDate = format(new Date(gig.dateAndTime.seconds*1000) ,'do MMMM Y')
return formattedGigDate === formattedDate
})
我已经编写了以下测试,但是为我的测试重写Filter函数似乎非常笨拙--有没有一种方法可以从它的组件listByDay.tsx
"提取"Filter?对于这样的测试,最好的库是什么?
test('should filter gigs by date', () => {
// create an array of gigs
const gigs = [
{ dateAndTime: { seconds: 1609459200 } }, // 1 September 2021
{ dateAndTime: { seconds: 1609545600 } }, // 2 September 2021
{ dateAndTime: { seconds: 1609632000 } }, // 3 September 2021
]
// create a new date object with the current date
const currentDate = new Date(2021, 8, 1) // 1 September 2021
// pass the gigs and currentDate to the filter function
const gigsToday = gigs.filter((gig) => {
const formattedDate = format(currentDate, 'do MMMM Y')
const formattedGigDate = format(new Date(gig.dateAndTime.seconds*1000) ,'do MMMM Y')
return formattedGigDate === formattedDate
})
// check that the filter function returned the correct gigs
expect(gigsToday).toBe([{ dateAndTime: { seconds: 1609459200 } }])
})