Skip to content

Commit

Permalink
certif(banner): extract information banners to dedicated component
Browse files Browse the repository at this point in the history
  • Loading branch information
HEYGUL committed Jan 16, 2025
1 parent 8429388 commit ee7d4d6
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 15 deletions.
11 changes: 11 additions & 0 deletions certif/app/components/information-banners.gjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import PixBannerAlert from '@1024pix/pix-ui/components/pix-banner-alert';

import textWithMultipleLang from '../helpers/text-with-multiple-lang.js';

<template>
{{#each @banners as |banner|}}
<PixBannerAlert @type={{banner.severity}} @canCloseBanner={{false}}>
{{textWithMultipleLang banner.message}}
</PixBannerAlert>
{{/each}}
</template>
2 changes: 1 addition & 1 deletion certif/app/models/banner.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Model, { attr } from '@ember-data/model';

export default class InformationBanner extends Model {
export default class Banner extends Model {
@attr() severity;
@attr() message;
}
2 changes: 1 addition & 1 deletion certif/app/models/information-banner.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Model, { hasMany } from '@ember-data/model';

export default class InformationBanner extends Model {
@hasMany('banners', { async: false, inverse: null }) banners;
@hasMany('banner', { async: false, inverse: null }) banners;
}
6 changes: 1 addition & 5 deletions certif/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@

<CommunicationBanner />

{{#each @model.informationBanner.banners as |banner|}}
<PixBannerAlert @type={{banner.severity}} @canCloseBanner={{false}}>
{{text-with-multiple-lang banner.message}}
</PixBannerAlert>
{{/each}}
<InformationBanners @banners={{@model.informationBanner.banners}} />

{{outlet}}

Expand Down
8 changes: 4 additions & 4 deletions certif/mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,11 @@ function routes() {

this.get('/information-banners/:target', (schema, request) => {
const { target } = request.params;
let informationBanners = schema.informationBanners.find(target);
if (!informationBanners) {
informationBanners = server.create('information-banner', { id: target, banners: [] });
const informationBanner = schema.informationBanners.find(target);
if (informationBanner) {
return informationBanner;
}
return informationBanners;
return server.create('information-banner', 'withoutBanners');
});
}

Expand Down
8 changes: 4 additions & 4 deletions certif/mirage/factories/information-banner.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Factory } from 'miragejs';
import { Factory, trait } from 'miragejs';

export default Factory.extend({
banners() {
return [];
},
withoutBanners: trait({
banners: [],
}),
});
7 changes: 7 additions & 0 deletions certif/mirage/serializers/information-banner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ApplicationSerializer from './application';

const include = ['banners'];

export default ApplicationSerializer.extend({
include,
});
42 changes: 42 additions & 0 deletions certif/tests/acceptance/application-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { visit } from '@1024pix/ember-testing-library';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { setupIntl } from 'ember-intl/test-support';
import { setupApplicationTest } from 'ember-qunit';
import { module, test } from 'qunit';

module('Application', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
setupIntl(hooks, 'fr');

module('When there are no information banners', function () {
test('it should not display any banner', async function (assert) {
// given
server.create('information-banner', 'withoutBanners', { id: 'pix-certif-local' });

// when
const screen = await visit(`/`);

// then
assert.dom(screen.queryByRole('alert')).doesNotExist();
});
});

module('When there is an information banner', function () {
test('it should display it', async function (assert) {
// given
const banner = server.create('banner', {
id: 'pix-certif-local:1',
severity: 'info',
message: '[en]some text[/en][fr]du texte[/fr]',
});
server.create('information-banner', { id: 'pix-certif-local', banners: [banner] });

// when
const screen = await visit(`/`);

// then
assert.dom(screen.getByRole('alert')).exists();
});
});
});
36 changes: 36 additions & 0 deletions certif/tests/integration/components/information-banners-test.gjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { render } from '@1024pix/ember-testing-library';
import { module, test } from 'qunit';
import InformationBanners from 'pix-certif/components/information-banners';
import setupIntlRenderingTest from '../../helpers/setup-intl-rendering';

module('Integration | Component | information-banners', function (hooks) {
setupIntlRenderingTest(hooks);

test('should not display the banner when no banners', async function (assert) {
// given
const banners = [];

// when
const screen = await render(<template><InformationBanners @banners={{banners}} /></template>);

// then
assert.dom(screen.queryByRole('alert')).doesNotExist();
});

test('should display the information banner', async function (assert) {
// given
const banners = [
{
severity: 'information',
message: '[fr]texte de la bannière d‘information[/fr][en]information banner text[/en]',
},
];

// when
const screen = await render(<template><InformationBanners @banners={{banners}} /></template>);

// then
assert.dom(screen.getByText('texte de la bannière d‘information')).exists();
assert.dom(screen.getByRole('alert')).exists();
});
});

0 comments on commit ee7d4d6

Please sign in to comment.