| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
{ |
| 8 |
QUnit.module( 've.dm.MWTransclusionModel', ve.test.utils.newMwEnvironment( { |
| 9 |
beforeEach() { |
| 10 |
|
| 11 |
this.server = this.sandbox.useFakeServer(); |
| 12 |
this.server.respondImmediately = true; |
| 13 |
|
| 14 |
} |
| 15 |
} ) ); |
| 16 |
|
| 17 |
const runAddPartTest = function ( assert, name, response, server, callback ) { |
| 18 |
const doc = ve.dm.Document.static.newBlankDocument(), |
| 19 |
transclusion = new ve.dm.MWTransclusionModel( doc ), |
| 20 |
part = ve.dm.MWTemplateModel.newFromName( transclusion, name ), |
| 21 |
done = assert.async(); |
| 22 |
|
| 23 |
server.respondWith( [ 200, { 'Content-Type': 'application/json' }, JSON.stringify( response ) ] ); |
| 24 |
|
| 25 |
transclusion.addPart( part ) |
| 26 |
.then( () => { |
| 27 |
callback( transclusion ); |
| 28 |
} ) |
| 29 |
.always( () => { |
| 30 |
done(); |
| 31 |
} ); |
| 32 |
}; |
| 33 |
|
| 34 |
QUnit.test( 'nextUniquePartId', ( assert ) => { |
| 35 |
const transclusion = new ve.dm.MWTransclusionModel(); |
| 36 |
assert.strictEqual( transclusion.nextUniquePartId(), 'part_0' ); |
| 37 |
assert.strictEqual( transclusion.nextUniquePartId(), 'part_1' ); |
| 38 |
assert.strictEqual( transclusion.nextUniquePartId(), 'part_2' ); |
| 39 |
} ); |
| 40 |
|
| 41 |
QUnit.test( 'fetch template part data', function ( assert ) { |
| 42 |
const response = { |
| 43 |
batchcomplete: '', |
| 44 |
pages: { |
| 45 |
1331311: { |
| 46 |
title: 'Template:Test', |
| 47 |
description: { en: 'MWTransclusionModel template test' }, |
| 48 |
params: { |
| 49 |
test: { |
| 50 |
label: { en: 'Test param' }, |
| 51 |
type: 'string', |
| 52 |
description: { en: 'This is a test param' }, |
| 53 |
required: false, |
| 54 |
suggested: false, |
| 55 |
example: null, |
| 56 |
deprecated: false, |
| 57 |
aliases: [], |
| 58 |
autovalue: null, |
| 59 |
default: null |
| 60 |
} |
| 61 |
}, |
| 62 |
paramOrder: [ 'test' ], |
| 63 |
format: 'inline', |
| 64 |
sets: [], |
| 65 |
maps: {} |
| 66 |
} |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
runAddPartTest( assert, 'Test', response, this.server, ( transclusion ) => { |
| 71 |
const parts = transclusion.getParts(), |
| 72 |
spec = parts[ 0 ].getSpec(); |
| 73 |
|
| 74 |
assert.strictEqual( parts.length, 1 ); |
| 75 |
assert.strictEqual( spec.getDescription( 'en' ), 'MWTransclusionModel template test' ); |
| 76 |
assert.strictEqual( spec.getParameterLabel( 'test', 'en' ), 'Test param' ); |
| 77 |
} ); |
| 78 |
} ); |
| 79 |
|
| 80 |
|
| 81 |
QUnit.test( 'fetch part data for parameterized template with no TemplateData', function ( assert ) { |
| 82 |
const response = { |
| 83 |
batchcomplete: '', |
| 84 |
pages: { |
| 85 |
1331311: { |
| 86 |
title: 'Template:NoData', |
| 87 |
notemplatedata: true, |
| 88 |
params: { |
| 89 |
foo: [], |
| 90 |
bar: [] |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
runAddPartTest( assert, 'NoData', response, this.server, ( transclusion ) => { |
| 97 |
const parts = transclusion.getParts(), |
| 98 |
spec = parts[ 0 ].getSpec(); |
| 99 |
|
| 100 |
assert.strictEqual( parts.length, 1 ); |
| 101 |
assert.deepEqual( spec.getKnownParameterNames(), [ 'foo', 'bar' ] ); |
| 102 |
} ); |
| 103 |
} ); |
| 104 |
|
| 105 |
QUnit.test( 'fetch part data for template with no TemplateData and no params', function ( assert ) { |
| 106 |
const response = { |
| 107 |
batchcomplete: '', |
| 108 |
pages: { |
| 109 |
1331311: { |
| 110 |
title: 'Template:NoParams', |
| 111 |
notemplatedata: true, |
| 112 |
params: [] |
| 113 |
} |
| 114 |
} |
| 115 |
}; |
| 116 |
|
| 117 |
runAddPartTest( assert, 'NoParams', response, this.server, ( transclusion ) => { |
| 118 |
const parts = transclusion.getParts(), |
| 119 |
spec = parts[ 0 ].getSpec(); |
| 120 |
|
| 121 |
assert.strictEqual( parts.length, 1 ); |
| 122 |
assert.deepEqual( spec.getKnownParameterNames(), [] ); |
| 123 |
} ); |
| 124 |
} ); |
| 125 |
} |