Skip to content

Commit 508352d

Browse files
authored
update readme to modern standards (#339)
* docs: first pass at readme update * docs: update test examples in readme
1 parent af23a1c commit 508352d

File tree

1 file changed

+76
-63
lines changed

1 file changed

+76
-63
lines changed

README.md

+76-63
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,17 @@ These will add the appropriate classes to the flash message component for stylin
7676
```javascript
7777
// Bootstrap: the flash message component will have 'alert alert-success' classes
7878
// Foundation: the flash message component will have 'alert-box success' classes
79-
import { get } from '@ember/object';
80-
81-
get(this, 'flashMessages').success('Success!');
79+
this.flashMessages.success('Success!');
8280
```
8381

8482
You can take advantage of Promises, and their `.then` and `.catch` methods. To add a flash message after saving a model (or when it fails):
8583

8684
```javascript
87-
import { get } from '@ember/object';
88-
8985
actions: {
9086
saveFoo() {
91-
const flashMessages = get(this, 'flashMessages');
87+
const flashMessages = this.flashMessages;
9288

93-
Ember.get(this, 'model')
89+
this.model
9490
.save()
9591
.then((res) => {
9692
flashMessages.success('Successfully saved!');
@@ -108,9 +104,7 @@ actions: {
108104
If the convenience methods don't fit your needs, you can add custom messages with `add`:
109105

110106
```javascript
111-
import { get } from '@ember/object';
112-
113-
get(this, 'flashMessages').add({
107+
this.flashMessages.add({
114108
message: 'Custom message'
115109
});
116110
```
@@ -119,9 +113,7 @@ get(this, 'flashMessages').add({
119113
You can also pass in options to custom messages:
120114

121115
```javascript
122-
import { get } from '@ember/object';
123-
124-
get(this, 'flashMessages').add({
116+
this.flashMessages.add({
125117
message: 'I like alpacas',
126118
type: 'alpaca',
127119
timeout: 500,
@@ -135,7 +127,7 @@ get(this, 'flashMessages').add({
135127
}
136128
});
137129

138-
get(this, 'flashMessages').success('This is amazing', {
130+
this.flashMessages.success('This is amazing', {
139131
timeout: 100,
140132
priority: 100,
141133
sticky: false,
@@ -233,13 +225,11 @@ Then animate using CSS transitions, using the `.active` and `.active.exiting` cl
233225
You can also add arbitrary options to messages:
234226

235227
```javascript
236-
import { get } from '@ember/object';
237-
238-
get(this, 'flashMessages').success('Cool story bro', {
228+
this.flashMessages.success('Cool story bro', {
239229
someOption: 'hello'
240230
});
241231

242-
get(this, 'flashMessages').add({
232+
this.flashMessages.add({
243233
message: 'hello',
244234
type: 'foo',
245235
componentName: 'some-component',
@@ -252,33 +242,29 @@ This makes use of the [component helper](http://emberjs.com/blog/2015/03/27/embe
252242

253243
```handlebars
254244
{{#each flashMessages.queue as |flash|}}
255-
{{#flash-message flash=flash as |component flash|}}
245+
<FlashMessage @flash={{flash}} as |component flash|>
256246
{{#if flash.componentName}}
257247
{{component flash.componentName content=flash.content}}
258248
{{else}}
259249
<h6>{{component.flashType}}</h6>
260250
<p>{{flash.message}}</p>
261251
{{/if}}
262-
{{/flash-message}}
252+
</FlashMessage>
263253
{{/each}}
264254
```
265255

266256
### Clearing all messages on screen
267257
It's best practice to use flash messages sparingly, only when you need to notify the user of something. If you're sending too many messages, and need a way for your users to clear all messages from screen, you can use this method:
268258

269259
```javascript
270-
import { get } from '@ember/object';
271-
272-
get(this, 'flashMessages').clearMessages();
260+
this.flashMessages.clearMessages();
273261
```
274262

275263
### Returning flash object
276264
The flash message service is designed to be Fluent, allowing you to chain methods on the service easily. The service should handle most cases but if you want to access the flash object directly, you can use the `getFlashObject` method:
277265

278266
```javascript
279-
import { get } from '@ember/object';
280-
281-
const flashObject = get(this, 'flashMessages').add({
267+
const flashObject = this.flashMessages.add({
282268
message: 'hola',
283269
type: 'foo'
284270
}).getFlashObject();
@@ -321,7 +307,7 @@ See the [options](#custom-messages-api) section for information about flash mess
321307

322308
Default: `[ 'success', 'info', 'warning', 'danger', 'alert', 'secondary' ]`
323309

324-
This option lets you specify exactly what types you need, which means in the above example, you can do `Ember.get('flashMessages').{alpaca,notice,foobar}`.
310+
This option lets you specify exactly what types you need, which means in the above example, you can do `this.flashMessages.{alpaca,notice,foobar}`.
325311

326312
- `preventDuplicates?: boolean`
327313

@@ -334,23 +320,23 @@ Then, to display somewhere in your app, add this to your template:
334320

335321
```handlebars
336322
{{#each flashMessages.queue as |flash|}}
337-
{{flash-message flash=flash}}
323+
<FlashMessage @flash={{flash}} />
338324
{{/each}}
339325
```
340326

341327
It also accepts your own template:
342328

343329
```handlebars
344330
{{#each flashMessages.queue as |flash|}}
345-
{{#flash-message flash=flash as |component flash|}}
331+
<FlashMessage @flash={{flash}} as |component flash|>
346332
<h6>{{component.flashType}}</h6>
347333
<p>{{flash.message}}</p>
348334
{{#if component.showProgressBar}}
349335
<div class="alert-progress">
350336
<div class="alert-progressBar" style="{{component.progressDuration}}"></div>
351337
</div>
352338
{{/if}}
353-
{{/flash-message}}
339+
</FlashMessage>
354340
{{/each}}
355341
```
356342

@@ -359,12 +345,12 @@ The `close` action is always passed to the component whether it is used or not.
359345

360346
When using a custom `close` action, you will want to set `destroyOnClick=false` to override the default (`destroyOnClick=true`). You could do this globally in `flashMessageDefaults`.
361347

362-
```
348+
```handlebars
363349
{{#each flashMessages.queue as |flash|}}
364-
{{#flash-message flash=flash as |component flash close|}}
350+
<FlashMessage @flash={{flash}} as |component flash close|>
365351
{{flash.message}}
366-
<a href="#" {{action close}}>x</a>
367-
{{/flash-message}}
352+
<span role="button" {{on "click" (action close)}}>x</span>
353+
</FlashMessage>
368354
{{/each}}
369355
```
370356

@@ -373,7 +359,7 @@ By default, flash messages will have Bootstrap style class names. If you want to
373359

374360
```handlebars
375361
{{#each flashMessages.queue as |flash|}}
376-
{{flash-message flash=flash messageStyle='foundation'}}
362+
<FlashMessage @flash={{flash}} @messageStyle='foundation' />
377363
{{/each}}
378364
```
379365

@@ -382,7 +368,7 @@ To display messages sorted by priority, add this to your template:
382368

383369
```handlebars
384370
{{#each flashMessages.arrangedQueue as |flash|}}
385-
{{flash-message flash=flash}}
371+
<FlashMessage @flash={{flash}} />
386372
{{/each}}
387373
```
388374

@@ -391,13 +377,13 @@ To add `radius` or `round` type corners in Foundation:
391377

392378
```handlebars
393379
{{#each flashMessages.arrangedQueue as |flash|}}
394-
{{flash-message flash=flash messageStyle='foundation' class='radius'}}
380+
<FlashMessage @flash={{flash}} @messageStyle='foundation' class='radius' />
395381
{{/each}}
396382
```
397383

398384
```handlebars
399385
{{#each flashMessages.arrangedQueue as |flash|}}
400-
{{flash-message flash=flash messageStyle='foundation' class='round'}}
386+
<FlashMessage @flash={{flash}} @messageStyle='foundation' class='round' />
401387
{{/each}}
402388
```
403389

@@ -406,7 +392,7 @@ If the provided component isn't to your liking, you can easily create your own.
406392

407393
```handlebars
408394
{{#each flashMessages.queue as |flash|}}
409-
{{custom-component flash=flash}}
395+
<CustomComponent @flash={{flash}} />
410396
{{/each}}
411397
```
412398

@@ -419,56 +405,83 @@ $ ember generate ember-cli-flash
419405

420406
This also adds the helper to `tests/test-helper.js`. You won't actually need to import this into your tests, but it's good to know what the blueprint does. Basically, the helper overrides the `_setInitialState` method so that the flash messages behave intuitively in a testing environment.
421407

408+
Some example tests below, based on qunit.
409+
422410
An example acceptance test:
423411

424412
```javascript
425-
// tests/acceptance/foo-test.js
413+
// tests/acceptance/foo-page-test.js
414+
415+
import { module, test } from 'qunit'
416+
import { setupApplicationTest } from 'ember-qunit'
417+
import { click, visit } from '@ember/test-helpers'
418+
419+
module('Application | Component | foo-page', function (hooks) {
420+
setupApplicationTest(hooks)
426421

427-
test('flash message is rendered', function(assert) {
428-
assert.expect(1);
429-
visit('/');
422+
test('flash message is rendered', async function(assert) {
423+
assert.expect(1);
430424

431-
andThen(() => assert.ok(find('.alert.alert-success').length));
425+
await visit('/');
426+
427+
await click('.button-that-opens-alert')
428+
429+
assert.dom('.alert.alert-success').exists({ count: 1 });
430+
});
432431
});
433432
```
434433

435434
An example integration test:
436435

437436
```javascript
438437
// tests/integration/components/x-foo-test.js
439-
import { getOwner } from '@ember/application';
440438

441-
moduleForComponent('x-foo', 'Integration | Component | x foo', {
442-
integration: true,
443-
beforeEach() {
444-
//We have to register any types we expect to use in this component
439+
import { module, test } from 'qunit'
440+
import { setupRenderingTest } from 'ember-qunit'
441+
import { render } from '@ember/test-helpers'
442+
import { hbs } from 'ember-cli-htmlbars'
443+
444+
module('Integration | Component | x-foo', function (hooks) {
445+
setupRenderingTest(hooks)
446+
447+
hooks.beforeEach(function() {
448+
// We have to register any types we expect to use in this component
445449
const typesUsed = ['info', 'warning', 'success'];
446-
getOwner(this).lookup('service:flash-messages').registerTypes(typesUsed);
447-
}
448-
});
450+
this.owner.lookup('service:flash-messages').registerTypes(typesUsed);
451+
})
449452

450-
test('it renders', function(assert) {
451-
...
453+
test('it renders', function(assert) {
454+
await render(hbs`<XFoo/>`)
455+
...
456+
})
452457
});
453458
```
454459

455460
## Unit testing
456461
For unit tests that require the `flashMessages` service, you'll need to do a small bit of setup:
457462

458463
```js
459-
import { getOwner } from '@ember/application';
464+
import { module, test } from 'qunit'
465+
import { setupTest } from 'ember-qunit'
460466

461-
moduleFor('route:foo', 'Unit | Route | foo', {
462-
needs: ['service:flash-messages', 'config:environment'],
463-
beforeEach() {
464-
const typesUsed = ['warning', 'success'];
465-
getOwner(this).lookup('service:flash-messages').registerTypes(typesUsed);
466-
}
467+
module('Container | Route | foo', function (hooks) {
468+
setupTest(hooks)
469+
470+
hooks.beforeEach(function() {
471+
// We have to register any types we expect to use in this component
472+
const typesUsed = ['info', 'warning', 'success'];
473+
this.owner.lookup('service:flash-messages').registerTypes(typesUsed);
474+
})
475+
476+
test('it does the thing it should do', function(assert) {
477+
const subject = this.owner.lookup('route:foo')
478+
...
479+
})
467480
});
468481
```
469482

470483
## Styling
471-
This addon is minimal and does not currently ship with a stylesheet. You can style flash messages by targeting the appropriate alert class (Foundation or Bootstrap) in your CSS.
484+
This addon is minimal and does not currently ship with a stylesheet. You can style flash messages by targeting the appropriate alert classes in your CSS.
472485

473486
## License
474487
[MIT](LICENSE.md)
@@ -507,4 +520,4 @@ We're grateful to these wonderful contributors who've contributed to `ember-cli-
507520
<a href="https://github.com/Darshan-Chauhan"><img src="https://avatars2.githubusercontent.com/u/18572215?v=4" title="Darshan-Chauhan" width="80" height="80"></a>
508521
<a href="https://github.com/dustinspecker"><img src="https://avatars3.githubusercontent.com/u/2449282?v=4" title="dustinspecker" width="80" height="80"></a>
509522

510-
[//]: contributor-faces
523+
[//]: contributor-faces

0 commit comments

Comments
 (0)