MDL-64061 javascript: Allow js_call_amd() be called without a function

Not all AMD modules need an explicit function call to start working. The
patch makes the function name optional, in which case the js_call_amd()
simply loads the module.
This commit is contained in:
David Mudrák
2018-11-16 14:31:57 +01:00
parent 8907290a46
commit fcee9eaafa
2 changed files with 57 additions and 20 deletions
+27
View File
@@ -102,4 +102,31 @@ class core_outputrequirementslib_testcase extends advanced_testcase {
// Make sure that the generated code does not contain backslashes.
$this->assertFalse(strpos($requirecode, '\\'), "Output contains backslashes: " . $requirecode);
}
/**
* Test AMD modules loading.
*/
public function test_js_call_amd() {
$page = new moodle_page();
// Load an AMD module without a function call.
$page->requires->js_call_amd('theme_foobar/lightbox');
// Load an AMD module and call its function without parameters.
$page->requires->js_call_amd('theme_foobar/demo_one', 'init');
// Load an AMD module and call its function with some parameters.
$page->requires->js_call_amd('theme_foobar/demo_two', 'init', [
'foo',
'keyWillIgnored' => 'baz',
[42, 'xyz'],
]);
$html = $page->requires->get_end_code();
$this->assertContains('require(["theme_foobar/lightbox"]);', $html);
$this->assertContains('require(["theme_foobar/demo_one"], function(amd) { amd.init(); });', $html);
$this->assertContains('require(["theme_foobar/demo_two"], function(amd) { amd.init("foo", "baz", [42,"xyz"]); });', $html);
}
}