Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TECH] Utiliser un service plutot que passer par le model pour synchroniser les traductions (PIX-14629) #771

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions pix-editor/app/adapters/phrase.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { inject as service } from '@ember/service';

export default class SynchronizeTranslationsController extends Controller {
@service notifications;
@service phrase;

@action
async importFromPhrase() {
try {
await this.model.download();
await this.phrase.download();
this.notifications.success('Téléchargement des traductions depuis Phrase effectué.');
} catch {
this.notifications.error('Erreur lors du téléchargement des traductions.');
Expand Down
10 changes: 0 additions & 10 deletions pix-editor/app/models/phrase.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default class SynchronizeTranslationsRoute extends Route {
@service store;

model() {
return this.store.createRecord('phrase');
}
}
export default class SynchronizeTranslationsRoute extends Route {}
15 changes: 15 additions & 0 deletions pix-editor/app/services/phrase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Service, { inject as service } from '@ember/service';
import fetch from 'fetch';

export default class PhraseService extends Service {
@service session;

async download(fetchFn = fetch) {
await fetchFn('/api/phrase/download', {
method: 'POST',
headers: {
authorization: `Bearer ${this.session.data.authenticated.apiKey}`,
},
});
}
}
38 changes: 38 additions & 0 deletions pix-editor/tests/unit/services/phrase-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Service from '@ember/service';
import { setupTest } from 'ember-qunit';
import { module, test } from 'qunit';
import sinon from 'sinon';

module('Unit | Service | storage', function(hooks) {
setupTest(hooks);

hooks.beforeEach(function() {
class sessionServiceStub extends Service {
get data() {
return { authenticated: { apiKey: 'someApiKey' } };
}
}
this.owner.register('service:session', sessionServiceStub);
});

module('download', function() {
test('it should trigger API call', async function(assert) {
// given
const phraseService = this.owner.lookup('service:phrase');
const fetch = sinon.stub().resolves();

// when
await phraseService.download(fetch);

// then
assert.ok(fetch.calledOnce);
assert.deepEqual(fetch.args[0], ['/api/phrase/download', {
method: 'POST',
headers: {
authorization: 'Bearer someApiKey',
},
}]);
});

});
});