Skip to content

Commit 753dd97

Browse files
committed
Add tests for crate report dialog
1 parent d44731f commit 753dd97

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { test, expect } from '@/e2e/helper';
2+
3+
test.describe('Acceptance | crate report dialog', { tag: '@acceptance' }, () => {
4+
test.beforeEach(async ({ mirage }) => {
5+
await mirage.addHook(server => {
6+
let crate = server.create('crate', { name: 'nanomsg' });
7+
server.create('version', { crate, num: '0.6.0' });
8+
});
9+
});
10+
11+
test('display a report form in dialog', async ({ page, percy, a11y }) => {
12+
await page.goto('/crates/nanomsg');
13+
await page.click('[data-test-report-button]');
14+
15+
const dialogContent = page.locator('[data-test-dialog-content]');
16+
await expect(dialogContent.locator('[data-test-reasons-group]')).toBeVisible();
17+
await expect(dialogContent.locator('[data-test-detail-group]')).toBeVisible();
18+
await expect(dialogContent.locator('[data-test-cancel]')).toHaveText('Cancel');
19+
await expect(dialogContent.locator('[data-test-report]')).toHaveText('Report');
20+
21+
await percy.snapshot();
22+
await a11y.audit();
23+
});
24+
25+
test('empty reasons selected shows an error', async ({ page }) => {
26+
await page.goto('/crates/nanomsg/');
27+
await page.click('[data-test-report-button]');
28+
29+
const dialogContent = page.locator('[data-test-dialog-content]');
30+
await dialogContent.locator('[data-test-report]').click();
31+
await expect(dialogContent.locator('[data-test-reasons-group] [data-test-error]')).toBeVisible();
32+
await expect(dialogContent.locator('[data-test-detail-group] [data-test-error]')).toHaveCount(0);
33+
});
34+
35+
test('other reason selected without given detail shows an error', async ({ page }) => {
36+
await page.goto('/crates/nanomsg/');
37+
await page.click('[data-test-report-button]');
38+
39+
const dialogContent = page.locator('[data-test-dialog-content]');
40+
await dialogContent.locator('[data-test-reason="spam"]').click();
41+
await dialogContent.locator('[data-test-reason="other"]').click();
42+
await dialogContent.locator('[data-test-report]').click();
43+
await expect(dialogContent.locator('[data-test-reasons-group] [data-test-error]')).toHaveCount(0);
44+
await expect(dialogContent.locator('[data-test-detail-group] [data-test-error]')).toBeVisible();
45+
});
46+
47+
test('valid report form should compose a mail and open', async ({ page }) => {
48+
// mock `window.open()`
49+
await page.addInitScript(() => {
50+
globalThis.open = (url, target, features) => {
51+
globalThis.openKwargs = { url, target, features };
52+
return { document: { write() {}, close() {} }, close() {} } as ReturnType<(typeof globalThis)['open']>;
53+
};
54+
});
55+
56+
await page.goto('/crates/nanomsg/');
57+
await page.click('[data-test-report-button]');
58+
59+
const dialogContent = page.locator('[data-test-dialog-content]');
60+
await dialogContent.locator('[data-test-reason="spam"]').click();
61+
await dialogContent.locator('[data-test-reason="other"]').click();
62+
await dialogContent.locator('[data-test-detail]').fill('test detail');
63+
await dialogContent.locator('[data-test-report]').click();
64+
65+
let body = `I'm reporting the https://crates.io/crates/nanomsg crate because:
66+
67+
- [x] it contains spam
68+
- [ ] it is name-squatting (reserving a crate name without content)
69+
- [ ] it is abusive or otherwise harmful
70+
- [ ] it contains a vulnerability (please try to contact the crate author first)
71+
- [x] it is violating the usage policy in some other way (please specify below)
72+
73+
Additional details:
74+
75+
test detail
76+
`;
77+
let subject = `The "nanomsg" crate`;
78+
let address = 'help@crates.io';
79+
let mailto = `mailto:${address}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
80+
// wait for `window.open()` to be called
81+
await page.waitForFunction(expect => globalThis.openKwargs.url === expect, mailto);
82+
await page.waitForFunction(expect => globalThis.openKwargs.target === expect, '_self');
83+
await expect(dialogContent).not.toBeVisible();
84+
});
85+
});
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { click, fillIn } from '@ember/test-helpers';
2+
import { module, test } from 'qunit';
3+
4+
import percySnapshot from '@percy/ember';
5+
6+
import { setupApplicationTest } from 'crates-io/tests/helpers';
7+
8+
import { visit } from '../helpers/visit-ignoring-abort';
9+
10+
module('Acceptance | create report dialog', function (hooks) {
11+
setupApplicationTest(hooks);
12+
setupWindowMock(hooks);
13+
14+
function prepare(context) {
15+
let server = context.server;
16+
let crate = server.create('crate', { name: 'nanomsg', newest_version: '0.6.0' });
17+
server.create('version', { crate, num: '0.6.0' });
18+
}
19+
20+
test('display a report form in dialog', async function (assert) {
21+
prepare(this);
22+
23+
await visit('/crates/nanomsg');
24+
await click('[data-test-report-button]');
25+
26+
assert.dom('[data-test-dialog-content] [data-test-reasons-group]').exists();
27+
assert.dom('[data-test-dialog-content] [data-test-detail-group]').exists();
28+
assert.dom('[data-test-dialog-content] [data-test-cancel]').hasText('Cancel');
29+
assert.dom('[data-test-dialog-content] [data-test-report]').hasText('Report');
30+
31+
await percySnapshot(assert);
32+
await a11yAudit(axeConfig);
33+
});
34+
35+
test('empty reasons selected shows an error', async function (assert) {
36+
prepare(this);
37+
38+
await visit('/crates/nanomsg');
39+
await click('[data-test-report-button]');
40+
41+
await fillIn('[data-test-name]', 'test detail');
42+
await click('[data-test-dialog-content] [data-test-report]');
43+
assert.dom('[data-test-dialog-content] [data-test-reasons-group] [data-test-error]').exists();
44+
assert.dom('[data-test-dialog-content] [data-test-detail-group] [data-test-error]').doesNotExist();
45+
});
46+
47+
test('other reason selected without given detail shows an error', async function (assert) {
48+
prepare(this);
49+
50+
await visit('/crates/nanomsg');
51+
await click('[data-test-report-button]');
52+
53+
await click('[data-test-dialog-content] [data-test-reason="spam"]');
54+
await click('[data-test-dialog-content] [data-test-reason="other"]');
55+
await click('[data-test-dialog-content] [data-test-report]');
56+
assert.dom('[data-test-dialog-content] [data-test-reasons-group] [data-test-error]').doesNotExist();
57+
assert.dom('[data-test-dialog-content] [data-test-detail-group] [data-test-error]').exists();
58+
});
59+
60+
test('valid report form should compose a mail and open', async function (assert) {
61+
prepare(this);
62+
let fakeWindow = { document: { write() {}, close() {} }, close() {} };
63+
window.open = (url, target, features) => {
64+
let body = `I'm reporting the https://crates.io/crates/nanomsg crate because:
65+
66+
- [x] it contains spam
67+
- [ ] it is name-squatting (reserving a crate name without content)
68+
- [ ] it is abusive or otherwise harmful
69+
- [ ] it contains a vulnerability (please try to contact the crate author first)
70+
- [x] it is violating the usage policy in some other way (please specify below)
71+
72+
Additional details:
73+
74+
test detail
75+
`;
76+
let subject = `The "nanomsg" crate`;
77+
let address = 'help@crates.io';
78+
let mailto = `mailto:${address}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
79+
assert.strictEqual(url, mailto);
80+
assert.strictEqual(target, '_self');
81+
return fakeWindow;
82+
};
83+
84+
await visit('/crates/nanomsg');
85+
await click('[data-test-report-button]');
86+
87+
await click('[data-test-dialog-content] [data-test-reason="spam"]');
88+
await click('[data-test-dialog-content] [data-test-reason="other"]');
89+
await fillIn('[data-test-name]', 'test detail');
90+
await click('[data-test-dialog-content] [data-test-report]');
91+
assert.dom('[data-test-dialog-content]').doesNotExist();
92+
});
93+
});

0 commit comments

Comments
 (0)