tests: Add CodexModule tests for full library mode
Also convert the data provider to a generator. Change-Id: Id9cfe88d789f655ae7fc26ce48c7bba08fbee33e
This commit is contained in:
parent
fe62ebafd2
commit
3211083394
2 changed files with 134 additions and 94 deletions
1
tests/phpunit/data/resourceloader/codex/codex.umd.cjs
Normal file
1
tests/phpunit/data/resourceloader/codex/codex.umd.cjs
Normal file
|
|
@ -0,0 +1 @@
|
|||
// Placeholder file for tests
|
||||
|
|
@ -16,8 +16,7 @@ class CodexModuleTest extends ResourceLoaderTestCase {
|
|||
public const FIXTURE_PATH = 'tests/phpunit/data/resourceloader/codex/';
|
||||
|
||||
public static function provideModuleConfig() {
|
||||
return [
|
||||
[ 'Codex subset',
|
||||
yield 'Codex subset' => [
|
||||
[
|
||||
'codexComponents' => [ 'CdxButton', 'CdxMessage', 'useModelWrapper' ],
|
||||
'codexStyleOnly' => false,
|
||||
|
|
@ -38,111 +37,151 @@ class CodexModuleTest extends ResourceLoaderTestCase {
|
|||
'_codex/CdxMessage.js',
|
||||
'_codex/useModelWrapper.js'
|
||||
],
|
||||
'styles' => [ 'CdxButton.css', 'CdxIcon.css', 'CdxMessage.css' ]
|
||||
'styles' => [ 'modules/CdxButton.css', 'modules/CdxIcon.css', 'modules/CdxMessage.css' ]
|
||||
]
|
||||
];
|
||||
yield 'Codex subset, style only' => [
|
||||
[
|
||||
'codexComponents' => [ 'CdxButton', 'CdxMessage' ],
|
||||
'codexStyleOnly' => true,
|
||||
'codexScriptOnly' => false
|
||||
],
|
||||
[ 'Codex subset, style only',
|
||||
[
|
||||
'codexComponents' => [ 'CdxButton', 'CdxMessage' ],
|
||||
'codexStyleOnly' => true,
|
||||
'codexScriptOnly' => false
|
||||
[
|
||||
'packageFiles' => [],
|
||||
'styles' => [ 'modules/CdxButton.css', 'modules/CdxIcon.css', 'modules/CdxMessage.css' ]
|
||||
]
|
||||
];
|
||||
yield 'Codex subset, script only' => [
|
||||
[
|
||||
'codexComponents' => [ 'CdxButton', 'CdxMessage', 'useModelWrapper' ],
|
||||
'codexStyleOnly' => false,
|
||||
'codexScriptOnly' => true
|
||||
],
|
||||
[
|
||||
'packageFiles' => [
|
||||
'codex.js',
|
||||
'_codex/constants.js',
|
||||
'_codex/useSlotContents2.js',
|
||||
'_codex/useWarnOnce.js',
|
||||
'_codex/useIconOnlyButton.js',
|
||||
'_codex/_plugin-vue_export-helper.js',
|
||||
'_codex/CdxButton.js',
|
||||
'_codex/useComputedDirection.js',
|
||||
'_codex/useComputedLanguage.js',
|
||||
'_codex/Icon.js',
|
||||
'_codex/CdxMessage.js',
|
||||
'_codex/useModelWrapper.js'
|
||||
],
|
||||
[
|
||||
'packageFiles' => [],
|
||||
'styles' => [ 'CdxButton.css', 'CdxIcon.css', 'CdxMessage.css' ]
|
||||
]
|
||||
'styles' => []
|
||||
]
|
||||
];
|
||||
yield 'Exception thrown when a chunk is requested' => [
|
||||
[
|
||||
'codexComponents' => [ 'CdxButton', 'buttonHelpers' ],
|
||||
],
|
||||
[ 'Codex subset, script only',
|
||||
[
|
||||
'codexComponents' => [ 'CdxButton', 'CdxMessage', 'useModelWrapper' ],
|
||||
'codexStyleOnly' => false,
|
||||
'codexScriptOnly' => true
|
||||
[
|
||||
'exception' => [
|
||||
'class' => InvalidArgumentException::class,
|
||||
'message' => '"buttonHelpers" is not an export of Codex and cannot be included in the "codexComponents" array.'
|
||||
]
|
||||
]
|
||||
];
|
||||
yield 'Exception thrown when a nonexistent file is requested' => [
|
||||
[
|
||||
'codexComponents' => [ 'CdxButton', 'blahblahidontexistblah' ],
|
||||
],
|
||||
[
|
||||
'exception' => [
|
||||
'class' => InvalidArgumentException::class,
|
||||
'message' => '"blahblahidontexistblah" is not an export of Codex and cannot be included in the "codexComponents" array.'
|
||||
]
|
||||
]
|
||||
];
|
||||
yield 'Exception thrown when codexComponents is empty in the module definition' => [
|
||||
[
|
||||
'codexComponents' => []
|
||||
],
|
||||
[
|
||||
'exception' => [
|
||||
'class' => InvalidArgumentException::class,
|
||||
'message' => "All 'codexComponents' properties in your module definition file " .
|
||||
'must either be omitted or be an array with at least one component name'
|
||||
]
|
||||
]
|
||||
];
|
||||
yield 'Exception thrown when codexComponents is not an array in the module definition' => [
|
||||
[
|
||||
'codexComponents' => ''
|
||||
],
|
||||
[
|
||||
'exception' => [
|
||||
'class' => InvalidArgumentException::class,
|
||||
'message' => "All 'codexComponents' properties in your module definition file " .
|
||||
'must either be omitted or be an array with at least one component name'
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
yield 'Exception thrown when the @wikimedia/codex module is required' => [
|
||||
[
|
||||
'codexComponents' => [ 'CdxButton', 'buttonHelpers' ],
|
||||
'dependencies' => [ '@wikimedia/codex' ]
|
||||
],
|
||||
[
|
||||
'exception' => [
|
||||
'class' => InvalidArgumentException::class,
|
||||
'message' => 'ResourceLoader modules using the CodexModule class cannot ' .
|
||||
"list the '@wikimedia/codex' module as a dependency. " .
|
||||
"Instead, use 'codexComponents' to require a subset of components."
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
yield 'Full library' => [
|
||||
[
|
||||
'codexFullLibrary' => true
|
||||
],
|
||||
[
|
||||
'packageFiles' => [
|
||||
'codex.js'
|
||||
],
|
||||
[
|
||||
'packageFiles' => [
|
||||
'codex.js',
|
||||
'_codex/constants.js',
|
||||
'_codex/useSlotContents2.js',
|
||||
'_codex/useWarnOnce.js',
|
||||
'_codex/useIconOnlyButton.js',
|
||||
'_codex/_plugin-vue_export-helper.js',
|
||||
'_codex/CdxButton.js',
|
||||
'_codex/useComputedDirection.js',
|
||||
'_codex/useComputedLanguage.js',
|
||||
'_codex/Icon.js',
|
||||
'_codex/CdxMessage.js',
|
||||
'_codex/useModelWrapper.js'
|
||||
],
|
||||
'styles' => []
|
||||
'styles' => [
|
||||
'codex.style.css'
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
yield 'Full library, script only' => [
|
||||
[
|
||||
'codexFullLibrary' => true,
|
||||
'codexScriptOnly' => true
|
||||
],
|
||||
[ 'Exception thrown when a chunk is requested',
|
||||
[
|
||||
'codexComponents' => [ 'CdxButton', 'buttonHelpers' ],
|
||||
[
|
||||
'packageFiles' => [
|
||||
'codex.js'
|
||||
],
|
||||
[
|
||||
'exception' => [
|
||||
'class' => InvalidArgumentException::class,
|
||||
'message' => '"buttonHelpers" is not an export of Codex and cannot be included in the "codexComponents" array.'
|
||||
]
|
||||
]
|
||||
'styles' => []
|
||||
]
|
||||
];
|
||||
|
||||
yield 'Full library, style only' => [
|
||||
[
|
||||
'codexFullLibrary' => true,
|
||||
'codexStyleOnly' => true
|
||||
],
|
||||
[ 'Exception thrown when a nonexistent file is requested',
|
||||
[
|
||||
'codexComponents' => [ 'CdxButton', 'blahblahidontexistblah' ],
|
||||
],
|
||||
[
|
||||
'exception' => [
|
||||
'class' => InvalidArgumentException::class,
|
||||
'message' => '"blahblahidontexistblah" is not an export of Codex and cannot be included in the "codexComponents" array.'
|
||||
]
|
||||
[
|
||||
'packageFiles' => [],
|
||||
'styles' => [
|
||||
'codex.style.css'
|
||||
]
|
||||
],
|
||||
[ 'Exception thrown when codexComponents is empty in the module definition',
|
||||
[
|
||||
'codexComponents' => []
|
||||
],
|
||||
[
|
||||
'exception' => [
|
||||
'class' => InvalidArgumentException::class,
|
||||
'message' => "All 'codexComponents' properties in your module definition file " .
|
||||
'must either be omitted or be an array with at least one component name'
|
||||
]
|
||||
]
|
||||
],
|
||||
[ 'Exception thrown when codexComponents is not an array in the module definition',
|
||||
[
|
||||
'codexComponents' => ''
|
||||
],
|
||||
[
|
||||
'exception' => [
|
||||
'class' => InvalidArgumentException::class,
|
||||
'message' => "All 'codexComponents' properties in your module definition file " .
|
||||
'must either be omitted or be an array with at least one component name'
|
||||
]
|
||||
]
|
||||
],
|
||||
[ 'Exception thrown when the @wikimedia/codex module is required',
|
||||
[
|
||||
'codexComponents' => [ 'CdxButton', 'buttonHelpers' ],
|
||||
'dependencies' => [ '@wikimedia/codex' ]
|
||||
],
|
||||
[
|
||||
'exception' => [
|
||||
'class' => InvalidArgumentException::class,
|
||||
'message' => 'ResourceLoader modules using the CodexModule class cannot ' .
|
||||
"list the '@wikimedia/codex' module as a dependency. " .
|
||||
"Instead, use 'codexComponents' to require a subset of components."
|
||||
]
|
||||
]
|
||||
],
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideModuleConfig
|
||||
*/
|
||||
public function testCodexSubset( $testCase, $moduleDefinition, $expected ) {
|
||||
public function testCodexSubset( $moduleDefinition, $expected ) {
|
||||
if ( isset( $expected['exception'] ) ) {
|
||||
$this->expectException( $expected['exception']['class'] );
|
||||
$this->expectExceptionMessage( $expected['exception']['message'] );
|
||||
|
|
@ -161,16 +200,16 @@ class CodexModuleTest extends ResourceLoaderTestCase {
|
|||
|
||||
// Style-only module will not have any packageFiles.
|
||||
$packageFilenames = isset( $packageFiles ) ? array_keys( $packageFiles[ 'files' ] ) : [];
|
||||
$this->assertEquals( $expected[ 'packageFiles' ] ?? [], $packageFilenames, 'Correct packageFiles added for ' . $testCase );
|
||||
$this->assertEquals( $expected[ 'packageFiles' ] ?? [], $packageFilenames, 'Correct packageFiles added' );
|
||||
|
||||
// Script-only module will not have any styleFiles.
|
||||
$styleFilenames = [];
|
||||
if ( count( $styleFiles ) > 0 ) {
|
||||
$styleFilenames = array_map( static function ( $filepath ) use ( $testModule ) {
|
||||
return str_replace( $testModule::CODEX_LIBRARY_DIR . 'modules/', '', $filepath->getPath() );
|
||||
return str_replace( $testModule::CODEX_LIBRARY_DIR, '', $filepath->getPath() );
|
||||
}, $styleFiles[ 'all' ] );
|
||||
}
|
||||
$this->assertEquals( $expected[ 'styles' ] ?? [], $styleFilenames, 'Correct styleFiles added for ' . $testCase );
|
||||
$this->assertEquals( $expected[ 'styles' ] ?? [], $styleFilenames, 'Correct styleFiles added' );
|
||||
}
|
||||
|
||||
public function testMissingCodexComponentsDefinition() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue