-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't choose first controller if no matching StimulusReflex-enabled c…
…ontroller was found (#670) This pull request solves an edge-case for scenarios when StimulusReflex tries to detect the right controller to use for performing the StimulusReflex `__perform` controller action for executing reflexes. For example, if an element had a `data-controller` and a `data-reflex` attribute, and the controller in `data-controller` was a StimulusReflex-enabled controller, but didn't match the Reflex specified in the `data-reflex` there were some cases where it wouldn't add the default `stimulus-reflex` controller to the element, but would still reference the `stimulus-reflex` controller in the `data-action` attribute. This basically lead to a no-op for the reflex action. Like: ```html <a data-controller=" some-sr-controller " data-reflex="click->Example#doSomething" ></a> ``` was expanded to this in some cases: ```diff <a data-controller=" some-sr-controller " data-reflex="click->Example#doSomething" + data-action="click->stimulus-reflex#__perform" ></a> ``` Whereas it should have been: ```diff <a data-controller=" some-sr-controller + stimulus-reflex " data-reflex="click->Example#doSomething" + data-action="click->stimulus-reflex#__perform" ></a> ``` With this, the `data-action` is not referencing an undefined Stimulus controller anymore and the reflex should execute again. ## Why should this be added For consitancy reasons, so that the declarative reflex syntax still continues to make sense and work as expected. Thanks to @foliosus for helping to debug this!
- Loading branch information
Showing
8 changed files
with
275 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
javascript/test/controllers.allReflexControllers.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { html, fixture, assert, nextFrame } from '@open-wc/testing' | ||
import refute from './refute' | ||
|
||
import ExampleController from './dummy/example_controller' | ||
import RegularController from './dummy/regular_controller' | ||
|
||
import { initialize } from '../stimulus_reflex' | ||
|
||
import App from '../app' | ||
import { application } from './dummy/application' | ||
import { allReflexControllers } from '../controllers' | ||
|
||
import { | ||
unloadAllControllers, | ||
registeredControllers, | ||
identifiers | ||
} from './test_helpers' | ||
|
||
describe('allReflexControllers', () => { | ||
beforeEach(() => { | ||
initialize(application) | ||
}) | ||
|
||
afterEach(() => { | ||
unloadAllControllers() | ||
}) | ||
|
||
it('returns StimulusReflex-enabled controller from parent', async () => { | ||
App.app.register('sr', ExampleController) | ||
|
||
assert.deepEqual(registeredControllers(), ['stimulus-reflex', 'sr']) | ||
|
||
const element = await fixture(html` | ||
<div data-controller="sr"> | ||
<a></a> | ||
</div> | ||
`) | ||
|
||
const a = element.querySelector('a') | ||
assert.deepEqual(identifiers(allReflexControllers(a)), ['sr']) | ||
}) | ||
|
||
it('doesnt return regular controller from parent', async () => { | ||
App.app.register('regular', RegularController) | ||
|
||
assert.deepEqual(registeredControllers(), ['stimulus-reflex', 'regular']) | ||
|
||
const element = await fixture(html` | ||
<div data-controller="regular"> | ||
<a></a> | ||
</div> | ||
`) | ||
|
||
const a = element.querySelector('a') | ||
assert.isEmpty(identifiers(allReflexControllers(a))) | ||
}) | ||
|
||
it('should return all reflex controllers from parents', async () => { | ||
App.app.register('sr-one', ExampleController) | ||
App.app.register('sr-two', ExampleController) | ||
App.app.register('regular-one', RegularController) | ||
App.app.register('regular-two', RegularController) | ||
|
||
const element = await fixture(html` | ||
<div data-controller="sr-one"> | ||
<div data-controller="sr-two"> | ||
<div data-controller="regular-one"> | ||
<div data-controller="regular-two"> | ||
<a></a> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
`) | ||
|
||
const a = element.querySelector('a') | ||
|
||
const controllers = allReflexControllers(a) | ||
|
||
assert.deepEqual(identifiers(controllers), ['sr-two', 'sr-one']) | ||
}) | ||
|
||
it('should return controllers with same name', async () => { | ||
App.app.register('sr', ExampleController) | ||
|
||
const outer = await fixture(html` | ||
<div data-controller="sr" id="outer"> | ||
<div data-controller="sr" id="inner"> | ||
<a></a> | ||
</div> | ||
</div> | ||
`) | ||
|
||
const a = outer.querySelector('a') | ||
const inner = outer.querySelector('#inner') | ||
const controllers = allReflexControllers(a) | ||
|
||
assert.deepEqual(identifiers(controllers), ['sr', 'sr']) | ||
|
||
assert.deepEqual(controllers[0].element, inner) | ||
assert.deepEqual(controllers[1].element, outer) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
javascript/test/controllers.localReflexControllers.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { html, fixture, assert, nextFrame, oneEvent } from '@open-wc/testing' | ||
|
||
import { application } from './dummy/application' | ||
import ExampleController from './dummy/example_controller' | ||
import RegularController from './dummy/regular_controller' | ||
|
||
import App from '../app' | ||
import { localReflexControllers } from '../controllers' | ||
import { initialize } from '../stimulus_reflex' | ||
|
||
import { | ||
unloadAllControllers, | ||
registeredControllers, | ||
identifiers | ||
} from './test_helpers' | ||
|
||
describe('localReflexControllers', () => { | ||
beforeEach(() => { | ||
initialize(application) | ||
}) | ||
|
||
afterEach(() => { | ||
unloadAllControllers() | ||
}) | ||
|
||
it('returns StimulusReflex-enabled controller', async () => { | ||
App.app.register('sr', ExampleController) | ||
|
||
assert.deepEqual(registeredControllers(), ['stimulus-reflex', 'sr']) | ||
|
||
const element = await fixture(html` | ||
<div data-controller="sr"></div> | ||
`) | ||
|
||
assert.deepEqual(identifiers(localReflexControllers(element)), ['sr']) | ||
}) | ||
|
||
it('doesnt return regular controller', async () => { | ||
App.app.register('sr', ExampleController) | ||
App.app.register('regular', RegularController) | ||
|
||
assert.deepEqual(registeredControllers(), [ | ||
'stimulus-reflex', | ||
'sr', | ||
'regular' | ||
]) | ||
|
||
const element = await fixture(html` | ||
<div data-controller="sr regular"></div> | ||
`) | ||
|
||
assert.deepEqual(identifiers(localReflexControllers(element)), ['sr']) | ||
}) | ||
|
||
it('returns all StimulusReflex-enabled controllers', async () => { | ||
App.app.register('sr-one', ExampleController) | ||
App.app.register('sr-two', ExampleController) | ||
App.app.register('regular-one', RegularController) | ||
App.app.register('regular-two', RegularController) | ||
|
||
assert.deepEqual(registeredControllers(), [ | ||
'stimulus-reflex', | ||
'sr-one', | ||
'sr-two', | ||
'regular-one', | ||
'regular-two' | ||
]) | ||
|
||
const element = await fixture(html` | ||
<div data-controller="regular-two sr-two sr-one regular-one"></div> | ||
`) | ||
|
||
assert.deepEqual(identifiers(localReflexControllers(element)), [ | ||
'sr-two', | ||
'sr-one' | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import App from '../app' | ||
|
||
export function registeredControllers () { | ||
return Array.from(App.app.router.modulesByIdentifier.keys()) | ||
} | ||
|
||
export function unloadAllControllers () { | ||
App.app.unload(registeredControllers()) | ||
} | ||
|
||
export function identifiers (controllers) { | ||
return controllers.map(controller => controller.identifier) | ||
} |