# Commands for Ember Implementation of the [Command](https://refactoring.guru/design-patterns/command) design pattern from [C-Q-S](https://en.wikipedia.org/wiki/Command–query_separation) for ember. - Commands are a primitives to be passed around - Commands are just functions - Commands are composable of many functions (but stays a function) - Commands can have/be a link (but stays a function) - Use commands with `(fn)` and enjoy partial applications (because it stayed a function) What you'll get: - A `Command` class to extend from for your implementation - A `LinkCommand` as syntactic sugar for creating a link (through [`ember-link`](https://github.com/buschtoens/ember-link)) - A `@command` decorator to connect your component with your command - A `` component as your building block to attach your command to the UI - The `` will accept a `Command`, an `@action` or a `(link)` - The `` will render the correct HTML element to take care of accessibility ## Installation ```bash ember install ember-command ``` ## Documentation - [Preparing UI Components](#preparing-ui-components) - [Writing Commands](#writing-commands) - [Ember Actions as Commands](#ember-actions-as-commands) - [Self-Contained Commands](#self-contained-commands) - [Seeding Commands](#seeding-commands) - [Compound Commands](#compound-commands) - [Link Commands](#link-commands) - [Attaching Commands to your UI](#attaching-commands-to-your-ui) - [Testing Commands](#testing-commands) This section will teach on **preparing** your UI components, **writing commands**, **attaching** them your UI and **testing** them. This documentation is guided by product development and engineering for _Super Rentals Inc_ with CPO _Tomster_ and Frontend Engineer _Zoey_. _These passages are optional, you are free to skip - but may feel very much related to your daily work._ > _Tomster_ realized a cohort of customers that are interested in seeing > personalized recommendations for rentals and put together a specification for > the feature. > Super Rentals shall be extended with a _recommendation_ section offering > personalized exposé to customers. Customers can _request offers_ and _learn > more_ about the object. > > _Tomster_ and _Zoey_ underlined the relevant nouns and verbs in the feature > specification to draw the domain terminology from it. The _recommendation_ is > the new aggregate and _request offer_ and _learn more_ are the actions upon > that. > > Meanwhile the backend developers were busy delievering an endpoint that > implements the business logic for these actions. Now _Zoey's_ job is to > connect the UI to these endpoints. To dispatch the request, the `data` > service is used. ### Preparing UI Components All elements/components that you want to invoke commands must be prepared. Gladly you don't have to deal with the implementation details, this is what the `` is for. Your job is to integrate this component into your existing set of components. [WAI-ARIA 1.1 for Accessible Rich Internet Applications](https://www.w3.org/TR/wai-aria-1.1) explicitely mentions `button`, `menuitem` and `link` as the implementationable roles for the abstract super role [`command`](https://www.w3.org/TR/wai-aria-1.1/#command) (but there also may be more UI elements, that are receivers of commands). Let's make an example button component with the help of ``: ```hbs {{! components/button/index.hbs }} {{yield}} ``` and give it an interface to describe the arguments: ```ts // components/button/index.d.ts import Component from '@glimmer/component'; import { Command } from 'ember-command/components/command-element'; export interface ButtonArgs { /** A command which will be invoked when the button is pushed */ push: Command; } ``` which we can use as: ```hbs ``` Yes a button is _pushed_ not _onClicked_ (Think about it: Do you _push_ or _click_ a button/switch to turn on the lights in your room?). > As _Zoey_ is caring about accessibility, she wants commands to be > represented as its appropriate element. Thanks to the `` the rendered element will adjust to either `` or ` .. and somewhere else .. ``` Of course, `requestOffer` can be any format mentioned under [writing commands](#writing-commands) section. Also for links, you have a chance to do this in a template-only style: ```hbs ``` Just use the flavor you like the most. ### Testing Commands As commands are isolated and self-containing a business logic, we can write tests to specifically test for this. Let's test the tracking command using [`ember-qunit-sinon`](https://github.com/elwayman02/ember-sinon-qunit) to stub our service: ```ts import { setupTest } from 'ember-qunit'; import { module, test } from 'qunit'; import { arrangeCommand } from 'ember-command/test-support'; import { TestContext } from 'ember-test-helpers'; import sinon from 'sinon'; import TrackingRequestOfferCommand from 'our-module'; module('Unit | Command | TrackingRequestOfferCommand', function (hooks) { setupTest(hooks); test('it tracks', async function (this: TestContext, assert) { this.owner.register('service:tracking', TrackingService); const trackingService = this.owner.lookup('service:tracking'); const stub = sinon.stub(trackingService, 'track'); const cmd = arrangeCommand(new TrackingRequestOfferCommand()); cmd.execute(); assert.ok(stub.calledOnce); }); }); ``` The `arrangeCommand` attach the owner and wires up dependency injection. There is also `arrangeCommandInstance` which is is the testing equivalent to the `@command` decorator rsp `(command)` helper to turn anything commandable into an instance. You can pass a `CommandInstance` to components expecting a command to be passed in: ```ts import { click, render } from '@ember/test-helpers'; import { setupTest } from 'ember-qunit'; import { module, test } from 'qunit'; import { hbs } from 'ember-cli-htmlbars'; import { arrangeCommandInstance } from 'ember-command/test-support'; import { TestContext } from 'ember-test-helpers'; import sinon from 'sinon'; import TrackingRequestOfferCommand from 'our-module'; module('Rendering | Component | ', function (hooks) { setupRenderingTest(hooks); test('it tracks', async function (this: TestContext, assert) { this.owner.register('service:tracking', TrackingService); const trackingService = this.owner.lookup('service:tracking'); const stub = sinon.stub(trackingService, 'track'); this.command = arrangeCommandInstance(new TrackingRequestOfferCommand()); await render(hbs``); await click('[data-test-recommendation]'); assert.ok(stub.calledOnce); }); }); ``` ## Contributing See the [Contributing](CONTRIBUTING.md) guide for details. ## License This project is licensed under the [MIT License](LICENSE.md).