Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Alerts #233

Merged
merged 6 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/tailwindcss-stimulus-components.cjs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/tailwindcss-stimulus-components.cjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/tailwindcss-stimulus-components.module.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/tailwindcss-stimulus-components.module.js.map

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions docs/alert.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,4 @@ Alerts are set up to slide into view from the top-right side of the screen. Clic
## Options

- `data-alert-dismiss-after-value` can be provided to make the alert dimiss after x miliseconds. Default is `undefined`.
- `data-alert-show-delay-value` can be set to tell the alert to show itself after x miliseconds. Defaults to `0` miliseconds.
- `data-alert-remove-delay-value` can be set to tell the alert to hide itself after x milisconds. Defaults to `1100` miliseconds.
- `data-alert-show-delay-value` can be set to tell the alert to show itself after x miliseconds. Defaults to `0` miliseconds.
1 change: 0 additions & 1 deletion src/alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default class extends Controller {
static values = {
dismissAfter: Number,
showDelay: { type: Number, default: 0 },
removeDelay: { type: Number, default: 1100 }
}

connect() {
Expand Down
49 changes: 49 additions & 0 deletions test/alert_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { aTimeout, fixture, expect, nextFrame } from '@open-wc/testing'
import { fetchFixture } from './test_helpers'

import { Application } from '@hotwired/stimulus'
import Alert from '../src/alert'

describe('AlertController', () => {
const loadFixture = async (fixturePath) => {
const html = await fetchFixture(fixturePath)
await fixture(html)
const application = Application.start()
application.register('alert', Alert)
}

const fetchElement = () => document.querySelector("[data-controller='alert'")

describe('with default values', () => {
it('shows the element immediately and closes it without delay ', async () => {
await loadFixture('alerts/alert_default.html')
expect(fetchElement().className.includes("hidden")).to.equal(false)

const closeButton = document.querySelector("[data-action='alert#close']")
closeButton.click()

await aTimeout(1000)
expect(fetchElement()).to.equal(null)
})
})

describe('with show delay value', () => {
it('shows after 1000ms', async () => {
await loadFixture('alerts/alert_show_delay.html')
expect(fetchElement().className.includes("hidden")).to.equal(true)

await aTimeout(1000)
expect(fetchElement().className.includes("hidden")).to.equal(false)
})
})

describe('with dismiss after value', () => {
it('dismisses after 500ms', async () => {
await loadFixture('alerts/alert_dismiss_after.html')
expect(fetchElement().className.includes("hidden")).to.equal(false)

await aTimeout(600)
expect(fetchElement()).to.equal(null)
})
})
})
6 changes: 6 additions & 0 deletions test/fixtures/alerts/alert_default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div data-controller="alert">
<div>
<div>Stimulus is the JS of the future!</div>
<button data-action="alert#close">Close</button>
</div>
</div>
6 changes: 6 additions & 0 deletions test/fixtures/alerts/alert_dismiss_after.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div data-controller="alert" data-alert-dismiss-after-value="500">
<div>
<div>Stimulus is the JS of the future!</div>
<button data-action="alert#close">Close</button>
</div>
</div>
10 changes: 10 additions & 0 deletions test/fixtures/alerts/alert_show_delay.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div
data-controller="alert"
data-alert-show-delay-value="1000"
class="hidden"
>
<div>
<div>Stimulus is the JS of the future!</div>
<button data-action="alert#close">Close</button>
</div>
</div>