Skip to content

Commit 0f41a0a

Browse files
chore: πŸ€– add test selectors to roles grants tests (#2749)
* chore: πŸ€– add test selectors to roles list tests * chore: πŸ€– add test selectors for roles global-scope tests * chore: πŸ€– add TABLE_RESOURCE_LINK to global-scope tests * test: πŸ’ change exists to isVisible * chore: πŸ€– add test selectors to roles grants tests * refactor: πŸ’‘ create custom selector for grants tests * refactor: πŸ’‘ use new FORM selector
1 parent 1025cbf commit 0f41a0a

File tree

2 files changed

+90
-78
lines changed

2 files changed

+90
-78
lines changed

β€Žui/admin/tests/acceptance/roles/grants-test.js

+78-78
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@
44
*/
55

66
import { module, test } from 'qunit';
7-
import {
8-
visit,
9-
currentURL,
10-
click,
11-
fillIn,
12-
find,
13-
findAll,
14-
} from '@ember/test-helpers';
7+
import { visit, currentURL, click, fillIn } from '@ember/test-helpers';
158
import { setupApplicationTest } from 'admin/tests/helpers';
169
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
1710
import a11yAudit from 'ember-a11y-testing/test-support/audit';
1811
import { Response } from 'miragejs';
1912
import { authenticateSession } from 'ember-simple-auth/test-support';
13+
import * as selectors from './selectors';
2014
import * as commonSelectors from 'admin/tests/helpers/selectors';
2115

2216
module('Acceptance | roles | grants', function (hooks) {
2317
setupApplicationTest(hooks);
2418
setupMirage(hooks);
2519

20+
// This is unique to permissions tests and only used once here
21+
const FIELD_GRANT_DISABLED = 'input[disabled]';
22+
23+
let grantsCount;
24+
2625
const instances = {
2726
scopes: {
2827
global: null,
@@ -31,14 +30,10 @@ module('Acceptance | roles | grants', function (hooks) {
3130
role: null,
3231
};
3332
const urls = {
34-
orgScope: null,
3533
roles: null,
3634
role: null,
3735
newRole: null,
3836
};
39-
const newGrantForm = 'form:nth-child(1)';
40-
const grantsForm = 'form:nth-child(2)';
41-
let grantsCount;
4237

4338
hooks.beforeEach(async function () {
4439
await authenticateSession({ username: 'admin' });
@@ -50,20 +45,19 @@ module('Acceptance | roles | grants', function (hooks) {
5045
instances.role = this.server.create('role', {
5146
scope: instances.scopes.org,
5247
});
53-
grantsCount = this.server.db.roles[0].grant_strings.length;
5448
urls.roles = `/scopes/${instances.scopes.org.id}/roles`;
5549
urls.role = `${urls.roles}/${instances.role.id}`;
5650
urls.grants = `${urls.role}/grants`;
51+
grantsCount = () =>
52+
this.server.schema.roles.all().models[0].grant_strings.length;
5753
});
5854

5955
test('visiting role grants', async function (assert) {
6056
await visit(urls.grants);
6157
await a11yAudit();
58+
6259
assert.strictEqual(currentURL(), urls.grants);
63-
assert.strictEqual(
64-
findAll(`${grantsForm} [name="grant"]`).length,
65-
grantsCount,
66-
);
60+
assert.dom(selectors.FIELD_GRANT).exists({ count: grantsCount() });
6761
});
6862

6963
test('cannot set grants without proper authorization', async function (assert) {
@@ -72,17 +66,13 @@ module('Acceptance | roles | grants', function (hooks) {
7266
);
7367
instances.role.update({ authorized_actions });
7468
await visit(urls.grants);
75-
assert.strictEqual(
76-
findAll('main form').length,
77-
1,
78-
'New grant form is not displayed.',
79-
);
80-
assert.notOk(
81-
find('main form button:not([type="submit"])'),
82-
'Grant delete button is not displayed.',
83-
);
84-
assert.notOk(find('.rose-form-actions'), 'Form actions are not displayed.');
85-
assert.ok(find('main form input[disabled]'), 'Grant fields are disabled.');
69+
70+
// This checks that only the form for existing grants is displayed
71+
assert.dom(commonSelectors.FORM).exists({ count: 1 });
72+
assert.dom(selectors.FIELD_NEW_GRANT_ADD_BTN).doesNotExist();
73+
assert.dom(FIELD_GRANT_DISABLED).exists({ count: grantsCount() });
74+
assert.dom(selectors.SAVE_BTN).doesNotExist();
75+
assert.dom(commonSelectors.CANCEL_BTN).doesNotExist();
8676
});
8777

8878
test('update a grant', async function (assert) {
@@ -93,26 +83,28 @@ module('Acceptance | roles | grants', function (hooks) {
9383
const attrs = JSON.parse(requestBody);
9484
assert.strictEqual(
9585
attrs.grant_strings[0],
96-
'ids=123,action=delete',
86+
selectors.FIELD_GRANT_VALUE,
9787
'A grant is updated',
9888
);
9989
const id = idMethod.split(':')[0];
10090
return { id };
10191
},
10292
);
10393
await visit(urls.grants);
104-
await fillIn(`${grantsForm} [name="grant"]`, 'ids=123,action=delete');
105-
await click('.rose-form-actions [type="submit"]:not(:disabled)');
94+
95+
await fillIn(selectors.FIELD_GRANT, selectors.FIELD_GRANT_VALUE);
96+
await click(selectors.SAVE_BTN);
10697
});
10798

10899
test('cancel a grant update', async function (assert) {
109100
await visit(urls.grants);
110-
await fillIn(`${grantsForm} [name="grant"]`, 'ids=123,action=delete');
111-
await click('.rose-form-actions button:not([type="submit"])');
112-
assert.notEqual(
113-
find(`${grantsForm} [name="grant"]`).value,
114-
'ids=123,action=delete',
115-
);
101+
102+
await fillIn(selectors.FIELD_GRANT, selectors.FIELD_GRANT_VALUE);
103+
await click(commonSelectors.CANCEL_BTN);
104+
105+
assert
106+
.dom(selectors.FIELD_GRANT)
107+
.doesNotIncludeText(selectors.FIELD_GRANT_VALUE);
116108
});
117109

118110
test('shows error message on grant update', async function (assert) {
@@ -129,12 +121,12 @@ module('Acceptance | roles | grants', function (hooks) {
129121
);
130122
});
131123
await visit(urls.grants);
132-
assert.strictEqual(
133-
findAll(`${grantsForm} [name="grant"]`).length,
134-
grantsCount,
135-
);
136-
await fillIn(`${grantsForm} [name="grant"]`, 'ids=123,action=delete');
137-
await click('.rose-form-actions [type="submit"]:not(:disabled)');
124+
125+
assert.dom(selectors.FIELD_GRANT).exists({ count: grantsCount() });
126+
127+
await fillIn(selectors.FIELD_GRANT, selectors.FIELD_GRANT_VALUE);
128+
await click(selectors.SAVE_BTN);
129+
138130
assert
139131
.dom(commonSelectors.ALERT_TOAST_BODY)
140132
.hasText('The request was invalid.');
@@ -148,25 +140,31 @@ module('Acceptance | roles | grants', function (hooks) {
148140
const attrs = JSON.parse(requestBody);
149141
assert.strictEqual(
150142
attrs.grant_strings.length,
151-
grantsCount + 1,
143+
grantsCount() + 1,
152144
'A grant is created',
153145
);
154146
const id = idMethod.split(':')[0];
155147
return { id };
156148
},
157149
);
158150
await visit(urls.grants);
159-
await fillIn(`${newGrantForm} [name="grant"]`, 'ids=123,action=delete');
160-
await click(`${newGrantForm} [type="submit"]:not(:disabled)`);
161-
await click('.rose-form-actions [type="submit"]:not(:disabled)');
151+
152+
await fillIn(selectors.FIELD_NEW_GRANT, selectors.FIELD_GRANT_VALUE);
153+
await click(selectors.FIELD_NEW_GRANT_ADD_BTN);
154+
await click(selectors.SAVE_BTN);
162155
});
163156

164157
test('cancel a grant creation', async function (assert) {
165158
await visit(urls.grants);
166-
await fillIn(`${newGrantForm} [name="grant"]`, 'ids=123,action=delete');
167-
await click(`${newGrantForm} [type="submit"]:not(:disabled)`);
168-
await click('.rose-form-actions button:not([type="submit"])');
169-
assert.notOk(find(`${newGrantForm} [name="grant"]`).value);
159+
160+
assert.dom(selectors.FIELD_GRANT).exists({ count: grantsCount() });
161+
162+
await fillIn(selectors.FIELD_NEW_GRANT, selectors.FIELD_GRANT_VALUE);
163+
await click(selectors.FIELD_NEW_GRANT_ADD_BTN);
164+
await click(commonSelectors.CANCEL_BTN);
165+
166+
assert.dom(selectors.FIELD_NEW_GRANT).hasNoText();
167+
assert.dom(selectors.FIELD_GRANT).exists({ count: grantsCount() });
170168
});
171169

172170
test('shows error message on grant create', async function (assert) {
@@ -183,34 +181,34 @@ module('Acceptance | roles | grants', function (hooks) {
183181
);
184182
});
185183
await visit(urls.grants);
186-
assert.strictEqual(
187-
findAll(`${grantsForm} [name="grant"]`).length,
188-
grantsCount,
189-
);
190-
await fillIn(`${newGrantForm} [name="grant"]`, 'ids=123,action=delete');
191-
await click(`${newGrantForm} [type="submit"]:not(:disabled)`);
192-
await click('.rose-form-actions [type="submit"]:not(:disabled)');
193-
assert.ok(find(commonSelectors.ALERT_TOAST_BODY));
184+
185+
assert.dom(selectors.FIELD_GRANT).exists({ count: grantsCount() });
186+
187+
await fillIn(selectors.FIELD_NEW_GRANT, selectors.FIELD_GRANT_VALUE);
188+
await click(selectors.FIELD_NEW_GRANT_ADD_BTN);
189+
await click(selectors.SAVE_BTN);
190+
191+
assert
192+
.dom(commonSelectors.ALERT_TOAST_BODY)
193+
.hasText('The request was invalid.');
194194
});
195195

196196
test('delete a grant', async function (assert) {
197197
await visit(urls.grants);
198-
await click(`${grantsForm} button:not([type="submit"])`);
199-
await click('.rose-form-actions [type="submit"]:not(:disabled)');
200-
assert.strictEqual(
201-
findAll(`${grantsForm} [name="grant"]`).length,
202-
grantsCount - 1,
203-
);
198+
199+
await click(selectors.FIELD_GRANT_REMOVE_BTN);
200+
await click(selectors.SAVE_BTN);
201+
202+
assert.dom(selectors.FIELD_GRANT).exists({ count: grantsCount() - 1 });
204203
});
205204

206205
test('cancel a grant remove', async function (assert) {
207206
await visit(urls.grants);
208-
await click(`${grantsForm} button`);
209-
await click('.rose-form-actions button:not([type="submit"])');
210-
assert.strictEqual(
211-
findAll(`${grantsForm} [name="grant"]`).length,
212-
grantsCount,
213-
);
207+
208+
await click(selectors.FIELD_GRANT_REMOVE_BTN);
209+
await click(commonSelectors.CANCEL_BTN);
210+
211+
assert.dom(selectors.FIELD_GRANT).exists({ count: grantsCount() });
214212
});
215213

216214
test('shows error message on grant remove', async function (assert) {
@@ -227,12 +225,14 @@ module('Acceptance | roles | grants', function (hooks) {
227225
);
228226
});
229227
await visit(urls.grants);
230-
assert.strictEqual(
231-
findAll(`${grantsForm} [name="grant"]`).length,
232-
grantsCount,
233-
);
234-
await click(`${grantsForm} button:not([type="submit"])`);
235-
await click('.rose-form-actions [type="submit"]:not(:disabled)');
236-
assert.ok(find(commonSelectors.ALERT_TOAST_BODY));
228+
229+
assert.dom(selectors.FIELD_GRANT).exists({ count: grantsCount() });
230+
231+
await click(selectors.FIELD_GRANT_REMOVE_BTN);
232+
await click(selectors.SAVE_BTN);
233+
234+
assert
235+
.dom(commonSelectors.ALERT_TOAST_BODY)
236+
.hasText('The request was invalid.');
237237
});
238238
});

β€Žui/admin/tests/acceptance/roles/selectors.js

+12
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,15 @@ export const REMOVE_ORG_ONLY_BTN = (name) =>
4343
`[data-test-manage-scopes-remove-${name}-modal] div:nth-child(3) button:last-of-type`;
4444
export const REMOVE_ORG_PROJECTS_BTN = (name) =>
4545
`[data-test-manage-scopes-remove-${name}-modal] div:nth-child(3) button`;
46+
47+
// Role grants
48+
export const FIELD_NEW_GRANT = 'form:nth-child(1) [name="grant"]';
49+
export const FIELD_NEW_GRANT_ADD_BTN = 'form:nth-child(1) [type="submit"]';
50+
export const FIELD_GRANT = 'form:nth-child(2) [name="grant"]';
51+
export const FIELD_GRANT_VALUE = 'ids=123,action=delete';
52+
export const FIELD_GRANT_REMOVE_BTN =
53+
'form:nth-child(2) button[title="Remove"]';
54+
export const GRANTS_FORM = 'form:nth-child(2)';
55+
// We need to use a different selector for the grants form because the
56+
// grants form has multiple submit buttons
57+
export const SAVE_BTN = '.rose-form-actions [type=submit]';

0 commit comments

Comments
Β (0)