From 4bd0623b35076c0fb3bef4b2a30d53f53a5199f6 Mon Sep 17 00:00:00 2001 From: Chris Manson Date: Tue, 25 Jun 2024 18:30:59 +0100 Subject: [PATCH] fix version dropdown and add a test --- addon/controllers/version.js | 74 +++++++++++++++++++++----------- tests/acceptance/version-test.js | 15 +++++++ 2 files changed, 63 insertions(+), 26 deletions(-) create mode 100644 tests/acceptance/version-test.js diff --git a/addon/controllers/version.js b/addon/controllers/version.js index 1d31456..dfeb9b6 100644 --- a/addon/controllers/version.js +++ b/addon/controllers/version.js @@ -1,39 +1,61 @@ -/* eslint-disable prettier/prettier, ember/no-classic-classes, ember/no-get, ember/no-actions-hash */ -import Controller, { - inject as controller, -} from '@ember/controller'; -import { get, computed } from '@ember/object'; -import { alias } from '@ember/object/computed'; +import Controller, { inject as controller } from '@ember/controller'; +import { action } from '@ember/object'; import { inject as service } from '@ember/service'; import compareVersions from 'compare-versions'; +import { deprecate } from '@ember/debug'; -export default Controller.extend({ - page: service(), - application: controller(), - store: service(), - router: service(), +export default class VersionController extends Controller { + @service page; + @controller application; - pages: alias('model.pages'), + @service store; + @service router; - versions: computed('application.model.allVersions.[]', function () { - let allVersions = get(this, 'application.model.allVersions'); + get pages() { + return this.model.pages; + } + + get versions() { + let allVersions = this.application.model.allVersions; - if(!allVersions) { - return; + if (!allVersions) { + return []; } return allVersions.sort(compareVersions).reverse(); - }), + } - actions: { - selectVersion(version) { - // Navigate to same section/page if it exists - const path = get(this, 'page.currentPage.url'); - this.store.queryRecord('content', {version, path}).then(() => { + @action + selectVersion(version) { + // Navigate to same section/page if it exists + const path = this.page.currentPage.url; + this.store + .queryRecord('content', { version, path }) + .then(() => { this.router.transitionTo(`/${version}/${path}`); - }).catch(() => { - this.router.transitionTo('version', version); }) - } + .catch(() => { + this.router.transitionTo('version', version); + }); } -}); + + // eslint-disable-next-line ember/no-actions-hash + actions = { + selectVersion: (version) => { + deprecate( + 'Use of the actions block in the guidemaker version controller is deprecated. If you are accessing controller.actions.selectVersion you can now access controller.selectVersion', + false, + { + id: 'guidemaker-version-controller-actions', + until: '5.0.0', + for: 'guidemaker', + since: { + available: '4.0.3', + enabled: '4.0.3', + }, + }, + ); + this.selectVersion(version); + }, + }; +} diff --git a/tests/acceptance/version-test.js b/tests/acceptance/version-test.js new file mode 100644 index 0000000..318798f --- /dev/null +++ b/tests/acceptance/version-test.js @@ -0,0 +1,15 @@ +import { module, test } from 'qunit'; +import { visit, currentURL } from '@ember/test-helpers'; +import { setupApplicationTest } from 'dummy/tests/helpers'; +import { selectChoose } from 'ember-power-select/test-support'; + +module('Acceptance | version', function (hooks) { + setupApplicationTest(hooks); + + test('the version dropdown works', async function (assert) { + await visit('/'); + await selectChoose('.ember-basic-dropdown-trigger', '1.1'); + + assert.strictEqual(currentURL(), '/v1.1.0'); + }); +});