Skip to content

Commit 8e680dc

Browse files
refactor: minor typing improvements (#2395)
This makes some minor typing improvements in our test code. Specifically instead of just `{...} as XBlock` which is an unsafe cast, we can use `{...} satisfies Partial<XBlock> as XBlock` which is a safer cast that lets you omit fields but requires that the fields you do include have the correct type.
1 parent 2f6e510 commit 8e680dc

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

src/course-outline/section-card/SectionCard.test.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ const subsection = {
3636
children: [{
3737
id: unit.id,
3838
}],
39-
},
40-
} as XBlock;
39+
} as any, // 'as any' because we are omitting a lot of fields from 'childInfo'
40+
} satisfies Partial<XBlock> as XBlock;
4141

4242
const section = {
4343
id: '123',
@@ -63,13 +63,14 @@ const section = {
6363
}],
6464
},
6565
}],
66-
},
66+
} as any, // 'as any' because we are omitting a lot of fields from 'childInfo'
6767
upstreamInfo: {
6868
readyToSync: true,
6969
upstreamRef: 'lct:org1:lib1:section:1',
7070
versionSynced: 1,
71+
errorMessage: null,
7172
},
72-
} as XBlock;
73+
} satisfies Partial<XBlock> as XBlock;
7374

7475
const onEditSectionSubmit = jest.fn();
7576

src/course-outline/subsection-card/SubsectionCard.test.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,14 @@ const subsection: XBlock = {
6666
children: [{
6767
id: unit.id,
6868
}],
69-
},
69+
} as any, // 'as any' because we are omitting a lot of fields from 'childInfo'
7070
upstreamInfo: {
7171
readyToSync: true,
7272
upstreamRef: 'lct:org1:lib1:subsection:1',
7373
versionSynced: 1,
74+
errorMessage: null,
7475
},
75-
} as XBlock;
76+
} satisfies Partial<XBlock> as XBlock;
7677

7778
const section: XBlock = {
7879
id: '123',
@@ -85,14 +86,14 @@ const section: XBlock = {
8586
children: [{
8687
id: subsection.id,
8788
}],
88-
},
89+
} as any, // 'as any' because we are omitting a lot of fields from 'childInfo'
8990
actions: {
9091
draggable: true,
9192
childAddable: true,
9293
deletable: true,
9394
duplicable: true,
9495
},
95-
} as XBlock;
96+
} satisfies Partial<XBlock> as XBlock;
9697

9798
const onEditSubectionSubmit = jest.fn();
9899

src/course-outline/unit-card/UnitCard.test.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const section = {
3131
deletable: true,
3232
duplicable: true,
3333
},
34-
} as XBlock;
34+
} satisfies Partial<XBlock> as XBlock;
3535

3636
const subsection = {
3737
id: '12',
@@ -45,7 +45,7 @@ const subsection = {
4545
deletable: true,
4646
duplicable: true,
4747
},
48-
} as XBlock;
48+
} satisfies Partial<XBlock> as XBlock;
4949

5050
const unit = {
5151
id: '123',
@@ -65,8 +65,9 @@ const unit = {
6565
readyToSync: true,
6666
upstreamRef: 'lct:org1:lib1:unit:1',
6767
versionSynced: 1,
68+
errorMessage: null,
6869
},
69-
} as XBlock;
70+
} satisfies Partial<XBlock> as XBlock;
7071

7172
const renderComponent = (props?: object) => render(
7273
<UnitCard

src/library-authoring/collections/CollectionDetails.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ describe('<CollectionDetails />', () => {
142142
{ blockType: 'Problem', count: 1 },
143143
{ blockType: 'Video', count: 0 },
144144
].forEach(({ blockType, count }) => {
145-
const blockCount = screen.getByText(blockType).closest('div') as HTMLDivElement;
145+
const blockCount = screen.getByText(blockType).closest('div')!;
146146
expect(within(blockCount).getByText(count.toString())).toBeInTheDocument();
147147
});
148148
});

src/library-authoring/component-info/ComponentPreview.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('<ComponentPreview />', () => {
3939
it('renders a preview of the component', async () => {
4040
initializeMocks();
4141
render();
42-
const iframe = (await screen.findByTitle('Preview')) as HTMLIFrameElement;
42+
const iframe = (await screen.findByTitle<HTMLIFrameElement>('Preview'));
4343
expect(iframe.src).toEqual(`http://localhost:18010/xblocks/v2/${usageKey}/embed/student_view/`);
4444
});
4545

0 commit comments

Comments
 (0)