-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathsections.test.ts
More file actions
170 lines (148 loc) · 6.5 KB
/
sections.test.ts
File metadata and controls
170 lines (148 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { describe, expect, it, mock } from 'bun:test';
import {
executeSectionsList,
executeSectionsGet,
executeSectionsSetPageMargins,
executeSectionsSetPageNumbering,
executeSectionsSetPageBorders,
executeSectionsSetHeaderFooterRef,
executeSectionsSetOddEvenHeadersFooters,
type SectionsAdapter,
} from './sections.js';
function makeAdapter(overrides: Partial<SectionsAdapter> = {}): SectionsAdapter {
const base: SectionsAdapter = {
list: () => ({
evaluatedRevision: '0',
total: 0,
items: [],
page: { limit: 250, offset: 0, returned: 0 },
}),
get: () => ({
address: { kind: 'section', sectionId: 'section-0' },
index: 0,
range: { startParagraphIndex: 0, endParagraphIndex: 0 },
}),
setBreakType: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
setPageMargins: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
setHeaderFooterMargins: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
setPageSetup: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
setColumns: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
setLineNumbering: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
setPageNumbering: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
setTitlePage: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
setOddEvenHeadersFooters: () => ({ success: true }),
setVerticalAlign: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
setSectionDirection: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
setHeaderFooterRef: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
clearHeaderFooterRef: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
setLinkToPrevious: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
setPageBorders: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
clearPageBorders: () => ({ success: true, section: { kind: 'section', sectionId: 'section-0' } }),
};
return {
...base,
...overrides,
};
}
describe('sections API validation', () => {
it('normalizes list defaults to limit=250 and offset=0', () => {
const list = mock(makeAdapter().list);
const adapter = makeAdapter({ list });
executeSectionsList(adapter);
expect(list).toHaveBeenCalledWith({ limit: 250, offset: 0 });
});
it('rejects invalid list limit', () => {
const adapter = makeAdapter();
expect(() => executeSectionsList(adapter, { limit: 0 })).toThrow(/limit must be a positive integer/i);
});
it('validates section address for sections.get', () => {
const adapter = makeAdapter();
expect(() => executeSectionsGet(adapter, { address: { kind: 'node', sectionId: 'x' } as any })).toThrow(
/must be a section address/i,
);
});
it('requires at least one field for setPageMargins', () => {
const adapter = makeAdapter();
expect(() =>
executeSectionsSetPageMargins(adapter, {
target: { kind: 'section', sectionId: 'section-0' },
}),
).toThrow(/requires at least one margin field/i);
});
it('rejects empty refId for setHeaderFooterRef', () => {
const adapter = makeAdapter();
expect(() =>
executeSectionsSetHeaderFooterRef(adapter, {
target: { kind: 'section', sectionId: 'section-0' },
kind: 'header',
variant: 'default',
refId: ' ',
}),
).toThrow(/must be a non-empty string/i);
});
it('accepts targetless odd/even settings mutation input', () => {
const setOddEvenHeadersFooters = mock(makeAdapter().setOddEvenHeadersFooters);
const adapter = makeAdapter({ setOddEvenHeadersFooters });
executeSectionsSetOddEvenHeadersFooters(adapter, { enabled: true }, { dryRun: true });
expect(setOddEvenHeadersFooters).toHaveBeenCalledWith({ enabled: true }, { changeMode: 'direct', dryRun: true });
});
it('requires at least one field for setPageNumbering', () => {
const adapter = makeAdapter();
expect(() =>
executeSectionsSetPageNumbering(adapter, {
target: { kind: 'section', sectionId: 'section-0' },
}),
).toThrow(/requires at least one of start, format, chapterStyle, or chapterSeparator/i);
});
it('accepts chapterStyle for setPageNumbering', () => {
const setPageNumbering = mock(makeAdapter().setPageNumbering);
const adapter = makeAdapter({ setPageNumbering });
executeSectionsSetPageNumbering(adapter, {
target: { kind: 'section', sectionId: 'section-0' },
chapterStyle: 1,
});
expect(setPageNumbering).toHaveBeenCalledWith(
{ target: { kind: 'section', sectionId: 'section-0' }, chapterStyle: 1 },
{ changeMode: 'direct', dryRun: false, expectedRevision: undefined },
);
});
it('accepts valid chapterSeparator for setPageNumbering', () => {
const setPageNumbering = mock(makeAdapter().setPageNumbering);
const adapter = makeAdapter({ setPageNumbering });
executeSectionsSetPageNumbering(adapter, {
target: { kind: 'section', sectionId: 'section-0' },
chapterSeparator: 'enDash',
});
expect(setPageNumbering).toHaveBeenCalledWith(
{ target: { kind: 'section', sectionId: 'section-0' }, chapterSeparator: 'enDash' },
{ changeMode: 'direct', dryRun: false, expectedRevision: undefined },
);
});
it('rejects invalid chapterSeparator for setPageNumbering', () => {
const adapter = makeAdapter();
expect(() =>
executeSectionsSetPageNumbering(adapter, {
target: { kind: 'section', sectionId: 'section-0' },
chapterSeparator: 'slash' as any,
}),
).toThrow(/chapterSeparator/i);
});
it('rejects chapterStyle less than 1 for setPageNumbering', () => {
const adapter = makeAdapter();
expect(() =>
executeSectionsSetPageNumbering(adapter, {
target: { kind: 'section', sectionId: 'section-0' },
chapterStyle: 0,
}),
).toThrow(/chapterStyle/i);
});
it('requires at least one field for setPageBorders', () => {
const adapter = makeAdapter();
expect(() =>
executeSectionsSetPageBorders(adapter, {
target: { kind: 'section', sectionId: 'section-0' },
borders: {},
}),
).toThrow(/requires at least one border field/i);
});
});