146 lines
3.5 KiB
JavaScript
146 lines
3.5 KiB
JavaScript
describe('library page', () => {
|
|
afterEach(() => {
|
|
delete global.Page
|
|
delete global.wx
|
|
jest.resetModules()
|
|
})
|
|
|
|
test('exposes mock categories and featured books by default', () => {
|
|
let capturedPageConfig
|
|
|
|
global.Page = config => {
|
|
capturedPageConfig = config
|
|
}
|
|
|
|
const libraryPageModule = require('../pages/library/index')
|
|
const pageData =
|
|
typeof libraryPageModule.createLibraryPageData === 'function'
|
|
? libraryPageModule.createLibraryPageData()
|
|
: capturedPageConfig?.data
|
|
|
|
expect(libraryPageModule.createLibraryPageData).toEqual(expect.any(Function))
|
|
expect(pageData.title).toBe('典籍')
|
|
expect(pageData.searchButtonText).toBe('搜索')
|
|
expect(pageData.activeCategoryId).toBe('featured')
|
|
expect(pageData.currentCategoryName).toBe('精选')
|
|
expect(pageData.categories).toHaveLength(9)
|
|
expect(pageData.categories.map(item => item.name)).toEqual([
|
|
'精选',
|
|
'注解',
|
|
'经论',
|
|
'伤寒金匮',
|
|
'医方',
|
|
'本草',
|
|
'针灸',
|
|
'医案',
|
|
'综合'
|
|
])
|
|
expect(pageData.visibleBooks).toHaveLength(6)
|
|
expect(pageData.visibleBooks[0]).toEqual(
|
|
expect.objectContaining({
|
|
id: 'huangdi-neijing-suwen',
|
|
title: '黄帝内经素问',
|
|
author: '王冰次',
|
|
dynasty: '唐',
|
|
period: '公元762年'
|
|
})
|
|
)
|
|
})
|
|
|
|
test('switches visible books when another category is selected', () => {
|
|
let capturedPageConfig
|
|
|
|
global.Page = config => {
|
|
capturedPageConfig = config
|
|
}
|
|
|
|
require('../pages/library/index')
|
|
|
|
const pageInstance = {
|
|
data: {
|
|
...(capturedPageConfig.data || {})
|
|
},
|
|
setData(update) {
|
|
this.data = {
|
|
...this.data,
|
|
...update
|
|
}
|
|
}
|
|
}
|
|
|
|
capturedPageConfig.handleCategoryTap.call(pageInstance, {
|
|
currentTarget: {
|
|
dataset: {
|
|
categoryId: 'annotation'
|
|
}
|
|
}
|
|
})
|
|
|
|
expect(pageInstance.data.activeCategoryId).toBe('annotation')
|
|
expect(pageInstance.data.currentCategoryName).toBe('注解')
|
|
expect(pageInstance.data.visibleBooks).toEqual([
|
|
expect.objectContaining({
|
|
title: '黄帝内经素问集注'
|
|
}),
|
|
expect.objectContaining({
|
|
title: '神农本草经辑注'
|
|
}),
|
|
expect.objectContaining({
|
|
title: '难经集注'
|
|
})
|
|
])
|
|
})
|
|
|
|
test('shows a toast while search is still mocked', () => {
|
|
let capturedPageConfig
|
|
|
|
global.Page = config => {
|
|
capturedPageConfig = config
|
|
}
|
|
|
|
global.wx = {
|
|
showToast: jest.fn()
|
|
}
|
|
|
|
require('../pages/library/index')
|
|
|
|
capturedPageConfig.handleSearchTap()
|
|
|
|
expect(global.wx.showToast).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
title: '搜索功能暂未开放',
|
|
icon: 'none'
|
|
})
|
|
)
|
|
})
|
|
|
|
test('adds a visible mingli-hall bridge entry to the library tab', () => {
|
|
let capturedPageConfig
|
|
|
|
global.Page = config => {
|
|
capturedPageConfig = config
|
|
}
|
|
|
|
global.wx = {
|
|
navigateTo: jest.fn(),
|
|
showToast: jest.fn()
|
|
}
|
|
|
|
const libraryPageModule = require('../pages/library/index')
|
|
const pageData = libraryPageModule.createLibraryPageData()
|
|
|
|
expect(pageData.domainBridge).toEqual(
|
|
expect.objectContaining({
|
|
title: '进入易学阁',
|
|
route: '/packages/mingli/pages/hall/index'
|
|
})
|
|
)
|
|
|
|
capturedPageConfig.handleDomainBridgeTap()
|
|
|
|
expect(global.wx.navigateTo).toHaveBeenCalledWith({
|
|
url: '/packages/mingli/pages/hall/index'
|
|
})
|
|
})
|
|
})
|