|
1 | 1 | # importmap_mocha-rails |
2 | 2 |
|
3 | | -This plugin makes it easy to test ES modules with [importmap-rails](https://github.com/rails/importmap-rails) when using Rails 7 or later. It integrates the [Mocha](https://mochajs.org/) JavaScript testing library (using [Chai](https://www.chaijs.com/) as the assertion library, [msw](https://mswjs.io/) as the mocking library) and runs tests for ES modules delivered with importmap in the browser. |
| 3 | +This plugin makes it easy to test ES modules with [importmap-rails](https://github.com/rails/importmap-rails) when using Rails 7 or later. |
| 4 | +It integrates the [Mocha](https://mochajs.org/) JavaScript testing library (using [Chai](https://www.chaijs.com/) as the assertion library, [@mswjs/interceptors](https://github.com/mswjs/interceptors) as the mocking library) and runs tests for ES modules delivered with importmap in the browser. |
4 | 5 |
|
5 | 6 | # Installation |
6 | 7 |
|
@@ -35,54 +36,45 @@ export default class extends Controller { |
35 | 36 | } |
36 | 37 | ``` |
37 | 38 |
|
38 | | -controllers/clear_controller.test.js |
| 39 | +controllers/clear_controller.spec.js |
39 | 40 |
|
40 | 41 | ```javascript |
41 | 42 | import { Application } from "@hotwired/stimulus" |
42 | 43 | import ClearController from 'controllers/clear_controller' |
43 | 44 |
|
| 45 | +const assert = chai.assert; |
44 | 46 | const html = `<div data-controller="clear"> |
45 | 47 | <input id="target" type="text" value="foo" data-clear-target="clear"> |
46 | 48 | <button data-action="clear#clear">test</button> |
47 | 49 | </div>` |
48 | 50 |
|
49 | | -const config = { attributes: true, childList: true, subtree: true }; |
50 | | - |
51 | | -// test!! test!! |
52 | | -describe('clear_controller', () => { |
| 51 | +describe('clear controller', () => { |
53 | 52 |
|
54 | 53 | let container; |
55 | 54 |
|
56 | | - beforeEach(() => { |
| 55 | + before(async () => { |
57 | 56 | container = document.getElementById('container') |
| 57 | + const app = Application.start(container); |
| 58 | + await app.register('clear', ClearController); |
| 59 | + |
58 | 60 | container.insertAdjacentHTML('afterbegin', html) |
59 | 61 | }); |
60 | 62 |
|
61 | | - afterEach(() => { |
| 63 | + after(() => { |
62 | 64 | const clone = container.cloneNode(false); |
63 | 65 | container.parentNode.replaceChild(clone, container); |
64 | 66 | }); |
65 | 67 |
|
66 | | - const watch = (fn) => { |
67 | | - const observer = new MutationObserver(fn); |
68 | | - observer.observe(container, config); |
69 | | - } |
70 | | - |
71 | 68 | describe('click', () => { |
72 | | - it('The value of input element is cleard', () => { |
73 | | - const app = Application.start(); |
74 | | - app.register('clear', ClearController); |
75 | | - |
| 69 | + it('The value of input element is cleard', async () => { |
76 | 70 | const target = container.querySelector('#target'); |
77 | | - |
78 | | - watch(() => target.value.to.equal('')); |
79 | | - |
80 | 71 | const button = container.querySelector('button'); |
81 | | - button.click() |
| 72 | + await button.click(); |
| 73 | + |
| 74 | + assert.equal('', target.value); |
82 | 75 | }); |
83 | 76 | }); |
84 | 77 | }); |
85 | | - |
86 | 78 | ``` |
87 | 79 |
|
88 | 80 | # Configuration |
|
0 commit comments