Skip to content

Commit

Permalink
Standard
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoroth committed Jul 14, 2023
1 parent d8948f8 commit c4f1252
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 58 deletions.
42 changes: 17 additions & 25 deletions javascript/test/controllers.allReflexControllers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import App from '../app'
import { application } from './dummy/application'
import { allReflexControllers } from '../controllers'

import { unloadAllControllers, registeredControllers, identifiers } from './test_helpers'
import {
unloadAllControllers,
registeredControllers,
identifiers
} from './test_helpers'

describe('allReflexControllers', () => {
beforeEach(() => {
Expand All @@ -22,12 +26,9 @@ describe('allReflexControllers', () => {
})

it('returns StimulusReflex-enabled controller from parent', async () => {
App.app.register("sr", ExampleController)
App.app.register('sr', ExampleController)

assert.deepEqual(registeredControllers(), [
"stimulus-reflex",
"sr"
])
assert.deepEqual(registeredControllers(), ['stimulus-reflex', 'sr'])

const element = await fixture(html`
<div data-controller="sr">
Expand All @@ -36,16 +37,13 @@ describe('allReflexControllers', () => {
`)

const a = element.querySelector('a')
assert.deepEqual(identifiers(allReflexControllers(a)), ["sr"])
assert.deepEqual(identifiers(allReflexControllers(a)), ['sr'])
})

it('doesnt return regular controller from parent', async () => {
App.app.register("regular", RegularController)
App.app.register('regular', RegularController)

assert.deepEqual(registeredControllers(), [
"stimulus-reflex",
"regular"
])
assert.deepEqual(registeredControllers(), ['stimulus-reflex', 'regular'])

const element = await fixture(html`
<div data-controller="regular">
Expand All @@ -58,10 +56,10 @@ describe('allReflexControllers', () => {
})

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)
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">
Expand All @@ -79,14 +77,11 @@ describe('allReflexControllers', () => {

const controllers = allReflexControllers(a)

assert.deepEqual(identifiers(controllers), [
"sr-two",
"sr-one",
])
assert.deepEqual(identifiers(controllers), ['sr-two', 'sr-one'])
})

it('should return controllers with same name', async () => {
App.app.register("sr", ExampleController)
App.app.register('sr', ExampleController)

const outer = await fixture(html`
<div data-controller="sr" id="outer">
Expand All @@ -100,10 +95,7 @@ describe('allReflexControllers', () => {
const inner = outer.querySelector('#inner')
const controllers = allReflexControllers(a)

assert.deepEqual(identifiers(controllers), [
"sr",
"sr",
])
assert.deepEqual(identifiers(controllers), ['sr', 'sr'])

assert.deepEqual(controllers[0].element, inner)
assert.deepEqual(controllers[1].element, outer)
Expand Down
12 changes: 9 additions & 3 deletions javascript/test/controllers.extractReflexName.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { findControllerByReflexName } from '../controllers'

describe('findControllerByReflexName', () => {
it('returns undefined if empty controllers array is passed', () => {
assert.isUndefined(findControllerByReflexName('click->TestReflex#create', []))
assert.isUndefined(
findControllerByReflexName('click->TestReflex#create', [])
)
assert.isUndefined(findControllerByReflexName('click->Test#create', []))
})

Expand All @@ -17,8 +19,12 @@ describe('findControllerByReflexName', () => {
{ identifier: 'last' }
]

assert.isUndefined(findControllerByReflexName('click->NoReflex#create', controllers))
assert.isUndefined(findControllerByReflexName('click->No#create', controllers))
assert.isUndefined(
findControllerByReflexName('click->NoReflex#create', controllers)
)
assert.isUndefined(
findControllerByReflexName('click->No#create', controllers)
)
})

it('returns matching controller', () => {
Expand Down
51 changes: 25 additions & 26 deletions javascript/test/controllers.localReflexControllers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import App from '../app'
import { localReflexControllers } from '../controllers'
import { initialize } from '../stimulus_reflex'

import { unloadAllControllers, registeredControllers, identifiers } from './test_helpers'
import {
unloadAllControllers,
registeredControllers,
identifiers
} from './test_helpers'

describe('localReflexControllers', () => {
beforeEach(() => {
Expand All @@ -20,60 +24,55 @@ describe('localReflexControllers', () => {
})

it('returns StimulusReflex-enabled controller', async () => {
App.app.register("sr", ExampleController)
App.app.register('sr', ExampleController)

assert.deepEqual(registeredControllers(), [
"stimulus-reflex",
"sr"
])
assert.deepEqual(registeredControllers(), ['stimulus-reflex', 'sr'])

const element = await fixture(html`
<div data-controller="sr"></div>
`)

assert.deepEqual(identifiers(localReflexControllers(element)), ["sr"])
assert.deepEqual(identifiers(localReflexControllers(element)), ['sr'])
})

it('doesnt return regular controller', async () => {
App.app.register("sr", ExampleController)
App.app.register("regular", RegularController)
App.app.register('sr', ExampleController)
App.app.register('regular', RegularController)

assert.deepEqual(registeredControllers(), [
"stimulus-reflex",
"sr",
"regular"
'stimulus-reflex',
'sr',
'regular'
])

const element = await fixture(html`
<div data-controller="sr regular"></div>
`)

assert.deepEqual(identifiers(localReflexControllers(element)), [
"sr"
])
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)
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"
'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"
'sr-two',
'sr-one'
])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,10 @@ describe('scanForReflexesOnElement', () => {
App.app.register('example', ExampleController)

const controllerElement = await fixture(html`
<div data-controller="example stimulus-reflex" data-reflex="click->Example#else"></div>
<div
data-controller="example stimulus-reflex"
data-reflex="click->Example#else"
></div>
`)

scanForReflexesOnElement(controllerElement)
Expand All @@ -339,13 +342,22 @@ describe('scanForReflexesOnElement', () => {
App.app.register('example', ExampleController)

const controllerElement = await fixture(html`
<div data-controller="example stimulus-reflex" data-reflex="click->Something#else"></div>
<div
data-controller="example stimulus-reflex"
data-reflex="click->Something#else"
></div>
`)

scanForReflexesOnElement(controllerElement)

assert.equal(controllerElement.dataset.controller, 'example stimulus-reflex')
assert.equal(
controllerElement.dataset.controller,
'example stimulus-reflex'
)
assert.equal(controllerElement.dataset.reflex, 'click->Something#else')
assert.equal(controllerElement.dataset.action, 'click->stimulus-reflex#__perform')
assert.equal(
controllerElement.dataset.action,
'click->stimulus-reflex#__perform'
)
})
})

0 comments on commit c4f1252

Please sign in to comment.