Skip to content

Commit 30f8d6c

Browse files
author
Takashi Kato
committed
use @mswjs/interceptors instead of msw, Fix sample test
1 parent 4f37de3 commit 30f8d6c

File tree

16 files changed

+1392
-346
lines changed

16 files changed

+1392
-346
lines changed

README.md

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# importmap_mocha-rails
22

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.
45

56
# Installation
67

@@ -35,54 +36,45 @@ export default class extends Controller {
3536
}
3637
```
3738

38-
controllers/clear_controller.test.js
39+
controllers/clear_controller.spec.js
3940

4041
```javascript
4142
import { Application } from "@hotwired/stimulus"
4243
import ClearController from 'controllers/clear_controller'
4344

45+
const assert = chai.assert;
4446
const html = `<div data-controller="clear">
4547
<input id="target" type="text" value="foo" data-clear-target="clear">
4648
<button data-action="clear#clear">test</button>
4749
</div>`
4850

49-
const config = { attributes: true, childList: true, subtree: true };
50-
51-
// test!! test!!
52-
describe('clear_controller', () => {
51+
describe('clear controller', () => {
5352

5453
let container;
5554

56-
beforeEach(() => {
55+
before(async () => {
5756
container = document.getElementById('container')
57+
const app = Application.start(container);
58+
await app.register('clear', ClearController);
59+
5860
container.insertAdjacentHTML('afterbegin', html)
5961
});
6062

61-
afterEach(() => {
63+
after(() => {
6264
const clone = container.cloneNode(false);
6365
container.parentNode.replaceChild(clone, container);
6466
});
6567

66-
const watch = (fn) => {
67-
const observer = new MutationObserver(fn);
68-
observer.observe(container, config);
69-
}
70-
7168
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 () => {
7670
const target = container.querySelector('#target');
77-
78-
watch(() => target.value.to.equal(''));
79-
8071
const button = container.querySelector('button');
81-
button.click()
72+
await button.click();
73+
74+
assert.equal('', target.value);
8275
});
8376
});
8477
});
85-
8678
```
8779

8880
# Configuration

app/controllers/importmap_mocha/test_controller.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
module ImportmapMocha
44
class TestController < ActionController::Base
55
layout false
6-
skip_before_action :verify_authenticity_token, only: :worker
76
def index; end
8-
9-
def worker
10-
end
117
end
128
end

app/views/importmap_mocha/test/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<% if Rails.application.config.importmap_mocha_scripts.size > 0 %>
77
<%= javascript_include_tag *Rails.application.config.importmap_mocha_scripts %>
88
<% end %>
9-
<%= javascript_include_tag 'mocha', 'chai', 'msw-1.1.0' %>
9+
<%= javascript_include_tag 'mocha', 'chai' %>
1010
<%= stylesheet_link_tag 'mocha' %>
1111
<%= javascript_importmap_tags 'importmap_mocha' %>
1212
<meta name="viewport" content="width=device-width, initial-scale=1">

app/views/importmap_mocha/test/worker.js

Lines changed: 0 additions & 303 deletions
This file was deleted.

0 commit comments

Comments
 (0)