2020-01-31 00:07:49 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWTransclusionModel tests.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2020-01-31 00:07:49 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-13 13:21:04 +00:00
|
|
|
{
|
2022-03-04 13:48:14 +00:00
|
|
|
QUnit.module( 've.dm.MWTransclusionModel', ve.test.utils.newMwEnvironment( {
|
2021-04-30 09:33:22 +00:00
|
|
|
beforeEach() {
|
2020-01-31 00:07:49 +00:00
|
|
|
// Mock XHR for mw.Api()
|
|
|
|
this.server = this.sandbox.useFakeServer();
|
|
|
|
this.server.respondImmediately = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
} ) );
|
|
|
|
|
2021-10-13 13:21:04 +00:00
|
|
|
const runAddPartTest = function ( assert, name, response, server, callback ) {
|
2021-04-29 14:42:18 +00:00
|
|
|
const doc = ve.dm.Document.static.newBlankDocument(),
|
2020-01-31 00:07:49 +00:00
|
|
|
transclusion = new ve.dm.MWTransclusionModel( doc ),
|
|
|
|
part = ve.dm.MWTemplateModel.newFromName( transclusion, name ),
|
|
|
|
done = assert.async();
|
|
|
|
|
|
|
|
server.respondWith( [ 200, { 'Content-Type': 'application/json' }, JSON.stringify( response ) ] );
|
|
|
|
|
|
|
|
transclusion.addPart( part )
|
2021-04-30 09:33:22 +00:00
|
|
|
.then( () => {
|
2020-01-31 00:07:49 +00:00
|
|
|
callback( transclusion );
|
|
|
|
} )
|
2021-04-30 09:33:22 +00:00
|
|
|
.always( () => {
|
2020-01-31 00:07:49 +00:00
|
|
|
done();
|
|
|
|
} );
|
2021-10-13 13:21:04 +00:00
|
|
|
};
|
2020-01-31 00:07:49 +00:00
|
|
|
|
2024-06-04 03:39:48 +00:00
|
|
|
QUnit.test( 'nextUniquePartId', ( assert ) => {
|
2021-08-27 11:00:35 +00:00
|
|
|
const transclusion = new ve.dm.MWTransclusionModel();
|
2022-04-19 11:46:34 +00:00
|
|
|
assert.strictEqual( transclusion.nextUniquePartId(), 'part_0' );
|
|
|
|
assert.strictEqual( transclusion.nextUniquePartId(), 'part_1' );
|
|
|
|
assert.strictEqual( transclusion.nextUniquePartId(), 'part_2' );
|
2021-08-27 11:00:35 +00:00
|
|
|
} );
|
|
|
|
|
2020-01-31 00:07:49 +00:00
|
|
|
QUnit.test( 'fetch template part data', function ( assert ) {
|
2021-04-29 14:42:18 +00:00
|
|
|
const response = {
|
2020-01-31 00:07:49 +00:00
|
|
|
batchcomplete: '',
|
|
|
|
pages: {
|
|
|
|
1331311: {
|
|
|
|
title: 'Template:Test',
|
|
|
|
description: { en: 'MWTransclusionModel template test' },
|
|
|
|
params: {
|
|
|
|
test: {
|
|
|
|
label: { en: 'Test param' },
|
|
|
|
type: 'string',
|
|
|
|
description: { en: 'This is a test param' },
|
|
|
|
required: false,
|
|
|
|
suggested: false,
|
|
|
|
example: null,
|
|
|
|
deprecated: false,
|
|
|
|
aliases: [],
|
|
|
|
autovalue: null,
|
|
|
|
default: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
paramOrder: [ 'test' ],
|
|
|
|
format: 'inline',
|
|
|
|
sets: [],
|
|
|
|
maps: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-30 09:33:22 +00:00
|
|
|
runAddPartTest( assert, 'Test', response, this.server, ( transclusion ) => {
|
2021-04-29 14:42:18 +00:00
|
|
|
const parts = transclusion.getParts(),
|
2020-01-31 00:07:49 +00:00
|
|
|
spec = parts[ 0 ].getSpec();
|
|
|
|
|
|
|
|
assert.strictEqual( parts.length, 1 );
|
|
|
|
assert.strictEqual( spec.getDescription( 'en' ), 'MWTransclusionModel template test' );
|
|
|
|
assert.strictEqual( spec.getParameterLabel( 'test', 'en' ), 'Test param' );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// T243868
|
|
|
|
QUnit.test( 'fetch part data for parameterized template with no TemplateData', function ( assert ) {
|
2021-04-29 14:42:18 +00:00
|
|
|
const response = {
|
2020-01-31 00:07:49 +00:00
|
|
|
batchcomplete: '',
|
|
|
|
pages: {
|
|
|
|
1331311: {
|
|
|
|
title: 'Template:NoData',
|
|
|
|
notemplatedata: true,
|
|
|
|
params: {
|
|
|
|
foo: [],
|
|
|
|
bar: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-30 09:33:22 +00:00
|
|
|
runAddPartTest( assert, 'NoData', response, this.server, ( transclusion ) => {
|
2021-04-29 14:42:18 +00:00
|
|
|
const parts = transclusion.getParts(),
|
2020-01-31 00:07:49 +00:00
|
|
|
spec = parts[ 0 ].getSpec();
|
|
|
|
|
|
|
|
assert.strictEqual( parts.length, 1 );
|
2021-06-28 10:53:45 +00:00
|
|
|
assert.deepEqual( spec.getKnownParameterNames(), [ 'foo', 'bar' ] );
|
2020-01-31 00:07:49 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'fetch part data for template with no TemplateData and no params', function ( assert ) {
|
2021-04-29 14:42:18 +00:00
|
|
|
const response = {
|
2020-01-31 00:07:49 +00:00
|
|
|
batchcomplete: '',
|
|
|
|
pages: {
|
|
|
|
1331311: {
|
|
|
|
title: 'Template:NoParams',
|
|
|
|
notemplatedata: true,
|
|
|
|
params: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-30 09:33:22 +00:00
|
|
|
runAddPartTest( assert, 'NoParams', response, this.server, ( transclusion ) => {
|
2021-04-29 14:42:18 +00:00
|
|
|
const parts = transclusion.getParts(),
|
2020-01-31 00:07:49 +00:00
|
|
|
spec = parts[ 0 ].getSpec();
|
|
|
|
|
|
|
|
assert.strictEqual( parts.length, 1 );
|
2021-06-28 10:53:45 +00:00
|
|
|
assert.deepEqual( spec.getKnownParameterNames(), [] );
|
2020-01-31 00:07:49 +00:00
|
|
|
} );
|
|
|
|
} );
|
2021-10-13 13:21:04 +00:00
|
|
|
}
|