|
1 | | -import { execFileSync } from 'child_process' |
| 1 | +/** @jest-environment node */ |
2 | 2 |
|
3 | | -const renderGalleryScript = ` |
4 | | - const React = (await import('react')).default |
5 | | - const { renderToStaticMarkup } = await import('react-dom/server') |
6 | | - const { NotionRenderer } = await import('react-notion-x') |
7 | | - const { Collection } = await import('react-notion-x/build/third-party/collection') |
8 | | - const createRecordMap = ({ |
9 | | - showPageIcon, |
10 | | - titleVisible, |
11 | | - legacyTitleVisible |
12 | | - }) => ({ |
13 | | - block: { |
14 | | - collection_view: { |
15 | | - value: { id: 'collection_view', type: 'collection_view', |
16 | | - collection_id: 'collection', view_ids: ['gallery_view'] } |
17 | | - }, |
18 | | - page: { |
19 | | - value: { id: 'page', type: 'page', parent_table: 'collection', |
20 | | - properties: { title: [['Gallery item']] }, |
21 | | - format: { page_icon: '📄' } } |
22 | | - } |
23 | | - }, |
24 | | - collection: { |
25 | | - collection: { |
26 | | - value: { id: 'collection', name: [['Gallery']], |
27 | | - schema: { title: { name: 'Name', type: 'title' } } } |
28 | | - } |
29 | | - }, |
30 | | - collection_view: { |
31 | | - gallery_view: { |
32 | | - value: { id: 'gallery_view', type: 'gallery', |
33 | | - format: { |
34 | | - collection_pointer: { id: 'collection' }, |
35 | | - ...(showPageIcon === undefined ? {} : { show_page_icon: showPageIcon }), |
36 | | - ...(legacyTitleVisible === undefined |
37 | | - ? {} |
38 | | - : { gallery_title_visible: legacyTitleVisible }), |
39 | | - gallery_cover: { type: 'none' }, |
40 | | - gallery_cover_size: 'medium', |
41 | | - gallery_cover_aspect: 'cover', |
42 | | - gallery_properties: |
43 | | - titleVisible === undefined |
44 | | - ? [] |
45 | | - : [{ property: 'title', visible: titleVisible }] |
46 | | - } } |
47 | | - } |
48 | | - }, |
49 | | - collection_query: { |
50 | | - collection: { |
51 | | - gallery_view: { collection_group_results: { blockIds: ['page'] } } |
52 | | - } |
53 | | - }, |
54 | | - signed_urls: {} |
55 | | - }) |
56 | | - const renderGallery = options => |
57 | | - renderToStaticMarkup( |
58 | | - React.createElement(NotionRenderer, { |
59 | | - recordMap: createRecordMap(options), |
60 | | - components: { Collection } |
61 | | - }) |
62 | | - ) |
63 | | - process.stdout.write(JSON.stringify({ |
64 | | - hidden: renderGallery({ titleVisible: false }), |
65 | | - titleVisible: renderGallery({ titleVisible: true }), |
66 | | - enabled: renderGallery({ showPageIcon: true, titleVisible: true }), |
67 | | - legacy: renderGallery({ legacyTitleVisible: true }) |
68 | | - })) |
69 | | -` |
| 3 | +import { galleryVisibilityClassName } from '@/lib/notion/galleryVisibilityClassName' |
70 | 4 |
|
71 | | -describe('Notion Gallery visibility settings', () => { |
72 | | - const result = JSON.parse( |
73 | | - execFileSync(process.execPath, [ |
74 | | - '--input-type=module', |
75 | | - '-e', |
76 | | - renderGalleryScript |
77 | | - ]) |
78 | | - ) |
| 5 | +const galleryView = format => ({ type: 'gallery', format }) |
79 | 6 |
|
| 7 | +describe('Notion Gallery visibility settings', () => { |
80 | 8 | it('hides omitted page icons and an explicitly hidden title', () => { |
81 | | - expect(result.hidden).toContain( |
82 | | - 'notion-gallery notion-gallery-hide-page-icons notion-gallery-hide-titles' |
83 | | - ) |
| 9 | + expect( |
| 10 | + galleryVisibilityClassName( |
| 11 | + galleryView({ |
| 12 | + gallery_properties: [{ property: 'title', visible: false }] |
| 13 | + }) |
| 14 | + ) |
| 15 | + ).toBe('notion-gallery-hide-page-icons notion-gallery-hide-titles') |
84 | 16 | }) |
85 | 17 |
|
86 | 18 | it('keeps a visible title while the omitted page-icon setting stays hidden', () => { |
87 | | - expect(result.titleVisible).toContain( |
88 | | - 'notion-gallery notion-gallery-hide-page-icons' |
89 | | - ) |
90 | | - expect(result.titleVisible).not.toContain('notion-gallery-hide-titles') |
| 19 | + expect( |
| 20 | + galleryVisibilityClassName( |
| 21 | + galleryView({ |
| 22 | + gallery_properties: [{ property: 'title', visible: true }] |
| 23 | + }) |
| 24 | + ) |
| 25 | + ).toBe('notion-gallery-hide-page-icons') |
91 | 26 | }) |
92 | 27 |
|
93 | 28 | it('keeps page icons visible when Notion explicitly enables them', () => { |
94 | | - expect(result.enabled).toContain('class="notion-gallery"') |
95 | | - expect(result.enabled).not.toContain('notion-gallery-hide-page-icons') |
96 | | - expect(result.enabled).not.toContain('notion-gallery-hide-titles') |
| 29 | + expect( |
| 30 | + galleryVisibilityClassName( |
| 31 | + galleryView({ |
| 32 | + show_page_icon: true, |
| 33 | + gallery_properties: [{ property: 'title', visible: true }] |
| 34 | + }) |
| 35 | + ) |
| 36 | + ).toBe('') |
97 | 37 | }) |
98 | 38 |
|
99 | 39 | it('preserves legacy Gallery data without the page-icon setting', () => { |
100 | | - expect(result.legacy).toContain('class="notion-gallery"') |
101 | | - expect(result.legacy).not.toContain('notion-gallery-hide-page-icons') |
| 40 | + expect( |
| 41 | + galleryVisibilityClassName(galleryView({ gallery_title_visible: true })) |
| 42 | + ).toBe('') |
102 | 43 | }) |
103 | 44 | }) |
0 commit comments