mediawiki-extensions-Visual.../modules/ve-mw/tests/ui/widgets/ve.ui.MWTransclusionOutlineContainerWidget.test.js
Thiemo Kreuz 115441b5d8 Add rather complex QUnit tests for transclusion outline code
These are more integration tests than actual "unit" tests. What
the tested code does depends a lot on e.g. how the model and
spec classes behave, and even on some events. Which is good. We
want to cover all of this with tests. The only question is: Is
there a good way to make these tests easier to read, while they
still cover the same code?

Bug: T289560
Change-Id: I8c681f161c272d143a07ca4d0080b4089b48bcb6
2021-08-28 12:34:36 +00:00

71 lines
3.2 KiB
JavaScript

QUnit.module( 've.ui.MWTransclusionOutlineContainerWidget' );
QUnit.test( 'Constructor', ( assert ) => {
const widget = new ve.ui.MWTransclusionOutlineContainerWidget();
// eslint-disable-next-line no-jquery/no-class-state
assert.ok( widget.$element.hasClass( 've-ui-mwTransclusionOutlineContainerWidget' ) );
assert.deepEqual( widget.partWidgets, {} );
} );
QUnit.test( 'Supports all ve.dm.MWTransclusionPartModel subclasses', ( assert ) => {
const transclusion = new ve.dm.MWTransclusionModel(),
widget = new ve.ui.MWTransclusionOutlineContainerWidget();
widget.onReplacePart( null, new ve.dm.MWTemplateModel( transclusion, {} ) );
widget.onReplacePart( null, new ve.dm.MWTemplatePlaceholderModel( transclusion ) );
widget.onReplacePart( null, new ve.dm.MWTransclusionContentModel( transclusion ) );
assert.ok( widget.partWidgets.part_0 instanceof ve.ui.MWTransclusionOutlineTemplateWidget );
assert.ok( widget.partWidgets.part_1 instanceof ve.ui.MWTransclusionOutlinePlaceholderWidget );
assert.ok( widget.partWidgets.part_2 instanceof ve.ui.MWTransclusionOutlineWikitextWidget );
} );
QUnit.test( 'Basic functionality', ( assert ) => {
const transclusion = new ve.dm.MWTransclusionModel(),
part0 = new ve.dm.MWTransclusionContentModel( transclusion ),
part1 = new ve.dm.MWTransclusionContentModel( transclusion ),
widget = new ve.ui.MWTransclusionOutlineContainerWidget();
widget.onReplacePart();
assert.deepEqual( widget.partWidgets, {} );
widget.onReplacePart( null, part0 );
widget.onReplacePart( null, part1 );
assert.deepEqual( Object.keys( widget.partWidgets ), [ 'part_0', 'part_1' ] );
widget.onReplacePart( part0 );
assert.deepEqual( Object.keys( widget.partWidgets ), [ 'part_1' ] );
widget.clear();
assert.deepEqual( widget.partWidgets, {} );
} );
QUnit.test( 'Adding and moving parts to specific positions', ( assert ) => {
const transclusion = new ve.dm.MWTransclusionModel(),
part0 = new ve.dm.MWTransclusionContentModel( transclusion ),
part1 = new ve.dm.MWTransclusionContentModel( transclusion ),
part2 = new ve.dm.MWTransclusionContentModel( transclusion ),
widget = new ve.ui.MWTransclusionOutlineContainerWidget();
// This adds the parts at an invalid position, at the start, and in the middle
widget.onReplacePart( null, part0, 666 );
widget.onReplacePart( null, part1, 0 );
widget.onReplacePart( null, part2, 1 );
// Note this is just a map and doesn't reflect the order in the UI
assert.deepEqual( Object.keys( widget.partWidgets ), [ 'part_0', 'part_1', 'part_2' ] );
assert.ok( widget.$element.children().eq( 0 ).is( widget.partWidgets.part_1.$element ) );
assert.ok( widget.$element.children().eq( 1 ).is( widget.partWidgets.part_2.$element ) );
assert.ok( widget.$element.children().eq( 2 ).is( widget.partWidgets.part_0.$element ) );
// This bypasses all logic in ve.dm.MWTransclusionModel, effectively making it a mock.
transclusion.parts = [ part2, part0, part1 ];
widget.onTransclusionModelChange( transclusion );
assert.ok( widget.$element.children().eq( 0 ).is( widget.partWidgets.part_2.$element ) );
assert.ok( widget.$element.children().eq( 1 ).is( widget.partWidgets.part_0.$element ) );
assert.ok( widget.$element.children().eq( 2 ).is( widget.partWidgets.part_1.$element ) );
} );