|
@@ -4,36 +4,7 @@ import { initStimulus } from './initStimulus';
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
/**
|
|
|
- * Example controller (shortcut method definitions object) from documentation
|
|
|
- */
|
|
|
-const wordCountController = {
|
|
|
- STATIC: {
|
|
|
- values: { max: { default: 10, type: Number } },
|
|
|
- },
|
|
|
- connect() {
|
|
|
- this.setupOutput();
|
|
|
- this.updateCount();
|
|
|
- },
|
|
|
- setupOutput() {
|
|
|
- if (this.output) return;
|
|
|
- const template = document.createElement('template');
|
|
|
- template.innerHTML = `<output name='word-count' for='${this.element.id}'></output>`;
|
|
|
- const output = template.content.firstChild;
|
|
|
- this.element.insertAdjacentElement('beforebegin', output);
|
|
|
- this.output = output;
|
|
|
- },
|
|
|
- updateCount(event) {
|
|
|
- const value = event ? event.target.value : this.element.value;
|
|
|
- const words = (value || '').split(' ');
|
|
|
- this.output.textContent = `${words.length} / ${this.maxValue} words`;
|
|
|
- },
|
|
|
- disconnect() {
|
|
|
- this.output && this.output.remove();
|
|
|
- },
|
|
|
-};
|
|
|
-
|
|
|
-/**
|
|
|
- * Example controller from documentation as an ES6 class
|
|
|
+ * Example controller.
|
|
|
*/
|
|
|
class WordCountController extends Controller {
|
|
|
static values = { max: { default: 10, type: Number } };
|
|
@@ -51,7 +22,7 @@ class WordCountController extends Controller {
|
|
|
setupOutput() {
|
|
|
if (this.output) return;
|
|
|
const template = document.createElement('template');
|
|
|
- template.innerHTML = `<output name='word-count' for='${this.element.id}' style='float: right;'></output>`;
|
|
|
+ template.innerHTML = `<output name='word-count' for='${this.element.id}' class='output-label'></output>`;
|
|
|
const output = template.content.firstChild;
|
|
|
this.element.insertAdjacentElement('beforebegin', output);
|
|
|
this.output = output;
|
|
@@ -115,52 +86,6 @@ describe('initStimulus', () => {
|
|
|
expect(application.controllers[0]).toBeInstanceOf(TestMockController);
|
|
|
});
|
|
|
|
|
|
- it('should support registering a controller via an object with the createController static method', async () => {
|
|
|
- const section = document.createElement('section');
|
|
|
- section.id = 'example-a';
|
|
|
- section.innerHTML = `<input value="some words" id="example-a-input" data-controller="example-a" data-action="change->example-a#updateCount" />`;
|
|
|
-
|
|
|
- // create a controller and register it
|
|
|
- application.register(
|
|
|
- 'example-a',
|
|
|
- application.constructor.createController(wordCountController),
|
|
|
- );
|
|
|
-
|
|
|
- // before controller element added - should not include an `output` element
|
|
|
- expect(document.querySelector('#example-a > output')).toEqual(null);
|
|
|
-
|
|
|
- document.querySelector('section').after(section);
|
|
|
-
|
|
|
- await Promise.resolve();
|
|
|
-
|
|
|
- // after controller connected - should have an output element
|
|
|
- expect(document.querySelector('#example-a > output').innerHTML).toEqual(
|
|
|
- '2 / 10 words',
|
|
|
- );
|
|
|
-
|
|
|
- await Promise.resolve();
|
|
|
-
|
|
|
- // should respond to changes on the input
|
|
|
- const input = document.querySelector('#example-a > input');
|
|
|
- input.setAttribute('value', 'even more words');
|
|
|
- input.dispatchEvent(new Event('change'));
|
|
|
-
|
|
|
- expect(document.querySelector('#example-a > output').innerHTML).toEqual(
|
|
|
- '3 / 10 words',
|
|
|
- );
|
|
|
-
|
|
|
- // removal of the input should also remove the output (disconnect method)
|
|
|
- input.remove();
|
|
|
-
|
|
|
- await Promise.resolve();
|
|
|
-
|
|
|
- // should call the disconnect method (removal of the injected HTML)
|
|
|
- expect(document.querySelector('#example-a > output')).toEqual(null);
|
|
|
-
|
|
|
- // clean up
|
|
|
- section.remove();
|
|
|
- });
|
|
|
-
|
|
|
it('should support the documented approach for registering a controller via a class with register', async () => {
|
|
|
const section = document.createElement('section');
|
|
|
section.id = 'example-b';
|
|
@@ -203,30 +128,4 @@ describe('initStimulus', () => {
|
|
|
// clean up
|
|
|
section.remove();
|
|
|
});
|
|
|
-
|
|
|
- it('should provide access to a base Controller class on the returned application instance', () => {
|
|
|
- expect(application.constructor.Controller).toEqual(Controller);
|
|
|
- });
|
|
|
-});
|
|
|
-
|
|
|
-describe('createController', () => {
|
|
|
- const createController = initStimulus().constructor.createController;
|
|
|
-
|
|
|
- it('should safely create a Stimulus Controller class if no args provided', () => {
|
|
|
- const CustomController = createController();
|
|
|
- expect(CustomController.prototype instanceof Controller).toBeTruthy();
|
|
|
- });
|
|
|
-
|
|
|
- it('should create a Stimulus Controller class with static properties', () => {
|
|
|
- const someMethod = jest.fn();
|
|
|
-
|
|
|
- const CustomController = createController({
|
|
|
- STATIC: { targets: ['source'] },
|
|
|
- someMethod,
|
|
|
- });
|
|
|
-
|
|
|
- expect(CustomController.targets).toEqual(['source']);
|
|
|
- expect(CustomController.someMethod).toBeUndefined();
|
|
|
- expect(CustomController.prototype.someMethod).toEqual(someMethod);
|
|
|
- });
|
|
|
});
|