Skip to content

Commit

Permalink
Add data-test attribute for description in tester-version template an…
Browse files Browse the repository at this point in the history
…d tests.
  • Loading branch information
rohitpaulk committed Oct 16, 2024
1 parent 9708729 commit bad1173
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 30 deletions.
2 changes: 1 addition & 1 deletion app/templates/course-admin/tester-version.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
{{/if}}
</div>

<div class="prose">
<div class="prose" data-test-description>
{{#if (gt @model.testerVersion.provisionedTestRunnersCount 0)}}
This tester version has
{{@model.testerVersion.provisionedTestRunnersCount}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import FakeActionCableConsumer from 'codecrafters-frontend/tests/support/fake-action-cable-consumer';
import testScenario from 'codecrafters-frontend/mirage/scenarios/test';
import testerVersionPage from 'codecrafters-frontend/tests/pages/course-admin/tester-version-page';
import testerVersionsPage from 'codecrafters-frontend/tests/pages/course-admin/tester-versions-page';
import { module, test } from 'qunit';
import { setupApplicationTest } from 'codecrafters-frontend/tests/helpers';
import { signInAsStaff } from 'codecrafters-frontend/tests/support/authentication-helpers';
import testerVersionPage from 'codecrafters-frontend/tests/pages/course-admin/tester-version-page';
import testerVersionsPage from 'codecrafters-frontend/tests/pages/course-admin/tester-versions-page';
import testScenario from 'codecrafters-frontend/mirage/scenarios/test';
import { waitUntil } from '@ember/test-helpers';

module('Acceptance | course-admin | tester-versions-page | deprovision-test-runners', function (hooks) {
setupApplicationTest(hooks);
Expand All @@ -12,33 +14,49 @@ module('Acceptance | course-admin | tester-versions-page | deprovision-test-runn
testScenario(this.server);
signInAsStaff(this.owner, this.server);

this.server.create('course-tester-version', {
const fakeActionCableConsumer = new FakeActionCableConsumer();
this.owner.register('service:action-cable-consumer', fakeActionCableConsumer, { instantiate: false });

const newTesterVersion = this.server.create('course-tester-version', {
activator: this.server.schema.users.first(),
course: this.server.schema.courses.findBy({ slug: 'redis' }),
commitSha: '1234567890',
createdAt: new Date(2021, 1, 1),
isLatest: false,
isActive: false,
provisionedTestRunnersCount: 4,
tagName: 'v10',
createdAt: new Date(2022, 1, 1),
isLatest: true,
isActive: true,
lastActivatedAt: new Date(2022, 1, 1),
provisionedTestRunnersCount: 0,
tagName: 'v11',
});

this.server.create('course-tester-version', {
const oldTesterVersion = this.server.create('course-tester-version', {
activator: this.server.schema.users.first(),
course: this.server.schema.courses.findBy({ slug: 'redis' }),
commitSha: '1234567890',
createdAt: new Date(2021, 1, 1),
isLatest: true,
isActive: true,
lastActivatedAt: new Date(2021, 1, 1),
provisionedTestRunnersCount: 0,
tagName: 'v11',
isLatest: false,
isActive: false,
provisionedTestRunnersCount: 4,
tagName: 'v10',
});

await testerVersionsPage.visit({ course_slug: 'redis' });
await testerVersionsPage.testerVersionListItem[1].viewTesterVersionButton.click();
assert.strictEqual(testerVersionPage.descriptionText, 'This tester version has 4 provisioned test runners.');

await testerVersionPage.deprovisionTestRunnersButton.click();
assert.ok(testerVersionPage.initiatedDeprovisioningNotice.isVisible, 'should show initiated deprovisioning notice');
assert.strictEqual(testerVersionPage.descriptionText, 'This tester version has 4 provisioned test runners.');

await waitUntil(() => fakeActionCableConsumer.hasSubscription('CourseTesterVersionChannel', { course_tester_version_id: oldTesterVersion.id }));

oldTesterVersion.provisionedTestRunnersCount = 2;
fakeActionCableConsumer.sendData('CourseTesterVersionChannel', { event: 'updated' });
await waitUntil(() => testerVersionPage.descriptionText === 'This tester version has 2 provisioned test runners.');

oldTesterVersion.provisionedTestRunnersCount = 0;
fakeActionCableConsumer.sendData('CourseTesterVersionChannel', { event: 'updated' });
await waitUntil(() => testerVersionPage.descriptionText === 'This tester version has no provisioned test runners.');

await testerVersionPage.clickOnBackToTesterVersionsListButton();
await testerVersionsPage.testerVersionListItem[0].viewTesterVersionButton.click();
Expand Down
8 changes: 4 additions & 4 deletions tests/acceptance/course-page/autofix-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module('Acceptance | course-page | autofix', function (hooks) {
await coursePage.testResultsBar.clickOnTab('AI Hints');
await coursePage.testResultsBar.autofixSection.clickOnStartAutofixButton();

await waitUntil(() => fakeActionCableConsumer.hasSubscriptionForChannel('LogstreamChannel'));
await waitUntil(() => fakeActionCableConsumer.hasSubscription('LogstreamChannel'));

const autofixRequest = this.server.schema.autofixRequests.first();
const logstream = this.server.schema.fakeLogstreams.first();
Expand Down Expand Up @@ -99,7 +99,7 @@ module('Acceptance | course-page | autofix', function (hooks) {

logstream.update({ isTerminated: true });
fakeActionCableConsumer.sendData('LogstreamChannel', { event: 'updated' });
await waitUntil(() => !fakeActionCableConsumer.hasSubscriptionForChannel('LogstreamChannel'));
await waitUntil(() => !fakeActionCableConsumer.hasSubscription('LogstreamChannel'));

await percySnapshot('Autofix - Success', { scope: '[data-test-test-results-bar]' });
});
Expand Down Expand Up @@ -133,7 +133,7 @@ module('Acceptance | course-page | autofix', function (hooks) {
await coursePage.testResultsBar.clickOnTab('AI Hints');
await coursePage.testResultsBar.autofixSection.clickOnStartAutofixButton();

await waitUntil(() => fakeActionCableConsumer.hasSubscriptionForChannel('LogstreamChannel'));
await waitUntil(() => fakeActionCableConsumer.hasSubscription('LogstreamChannel'));

const autofixRequest = this.server.schema.autofixRequests.first();
const logstream = this.server.schema.fakeLogstreams.first();
Expand All @@ -146,7 +146,7 @@ module('Acceptance | course-page | autofix', function (hooks) {
logstream.update({ isTerminated: true });
fakeActionCableConsumer.sendData('LogstreamChannel', { event: 'updated' });

await waitUntil(() => !fakeActionCableConsumer.hasSubscriptionForChannel('LogstreamChannel'));
await waitUntil(() => !fakeActionCableConsumer.hasSubscription('LogstreamChannel'));

await percySnapshot('Autofix - Failure', { scope: '[data-test-test-results-bar]' });
assert.strictEqual(1, 1); // Add at least one assertion
Expand Down
3 changes: 2 additions & 1 deletion tests/pages/course-admin/tester-version-page.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { attribute, clickable, create, visitable } from 'ember-cli-page-object';
import { attribute, clickable, create, text, visitable } from 'ember-cli-page-object';

export default create({
activateTesterVersionButton: {
scope: '[data-test-activate-tester-version-button]',
},

clickOnBackToTesterVersionsListButton: clickable('[data-test-back-to-tester-versions-list-button]'),
descriptionText: text('[data-test-description]'),

deprovisionTestRunnersButton: {
scope: '[data-test-deprovision-test-runners-button]',
Expand Down
23 changes: 14 additions & 9 deletions tests/support/fake-action-cable-consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ type SubscriptionCallbacks = {
};

export default class FakeActionCableConsumer {
#subscriptions: Record<string, SubscriptionCallbacks> = {};
#subscriptions: Record<string, { id: string; args: Record<string, string>; callbacks: SubscriptionCallbacks }[]> = {};

hasSubscription(channel: string, args?: Record<string, string>): boolean {
if (args) {
return !!this.#subscriptions[channel]?.some((subscription) => JSON.stringify(subscription.args) === JSON.stringify(args));
}

hasSubscriptionForChannel(channel: string): boolean {
return !!this.#subscriptions[channel];
}

Expand All @@ -20,20 +24,21 @@ export default class FakeActionCableConsumer {
throw new Error(`No subscription for channel ${channel}`);
}

const onDataCallback = this.#subscriptions[channel]?.onData;

if (onDataCallback) {
onDataCallback(data);
for (const subscription of this.#subscriptions[channel] || []) {
subscription.callbacks.onData?.(data);
}
}

subscribe(channel: string, _args: Record<string, string> = {}, callbacks: SubscriptionCallbacks): ActionCableSubscription {
this.#subscriptions[channel] = callbacks;
subscribe(channel: string, args: Record<string, string> = {}, callbacks: SubscriptionCallbacks): ActionCableSubscription {
const id = crypto.randomUUID();

this.#subscriptions[channel] ||= [];
this.#subscriptions[channel] = [...this.#subscriptions[channel], { id, args, callbacks }];

return {
send: () => {},
unsubscribe: () => {
delete this.#subscriptions[channel];
this.#subscriptions[channel] = this.#subscriptions[channel]!.filter((subscription) => subscription.id !== id);
},
};
}
Expand Down

0 comments on commit bad1173

Please sign in to comment.