Skip to content

Commit

Permalink
make the user's language available in Enketo forms
Browse files Browse the repository at this point in the history
  • Loading branch information
njogz authored Apr 2, 2022
1 parent e1fc99e commit 3c2f599
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 10 additions & 4 deletions webapp/src/ts/services/enketo-prepopulation-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isString as _isString } from 'lodash-es';

import { EnketoTranslationService } from '@mm-services/enketo-translation.service';
import { UserSettingsService } from '@mm-services/user-settings.service';
import { LanguageService } from '@mm-services/language.service';

@Injectable({
providedIn: 'root'
Expand All @@ -11,16 +12,20 @@ export class EnketoPrepopulationDataService {
constructor(
private enketoTranslationService:EnketoTranslationService,
private userSettingsService:UserSettingsService,
private languageService:LanguageService,
) {}

get(model, data) {
if (data && _isString(data)) {
return Promise.resolve(data);
}

return this.userSettingsService
.get()
.then((user) => {
return Promise
.all([
this.userSettingsService.get(),
this.languageService.get()
])
.then(([user, language]) => {
const xml = $($.parseXML(model));
const bindRoot = xml.find('model instance').children().first();

Expand All @@ -34,7 +39,8 @@ export class EnketoPrepopulationDataService {
}

if (userRoot.length) {
this.enketoTranslationService.bindJsonToXml(userRoot, user);
const userObject = { ...user, language };
this.enketoTranslationService.bindJsonToXml(userRoot, userObject);
}

return new XMLSerializer().serializeToString(bindRoot[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { expect, assert } from 'chai';

import { EnketoPrepopulationDataService } from '@mm-services/enketo-prepopulation-data.service';
import { UserSettingsService } from '@mm-services/user-settings.service';
import { LanguageService } from '@mm-services/language.service';

describe('EnketoPrepopulationData service', () => {
let service;
let UserSettings;
let languageSettings;

const generatedForm =
'<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
Expand Down Expand Up @@ -37,6 +39,7 @@ describe('EnketoPrepopulationData service', () => {
'<inputs>' +
'<user>' +
'<name/>' +
'<language/>' +
'</user>' +
'<meta>' +
'<location>' +
Expand Down Expand Up @@ -140,9 +143,11 @@ describe('EnketoPrepopulationData service', () => {

beforeEach(() => {
UserSettings = sinon.stub();
languageSettings = sinon.stub();
TestBed.configureTestingModule({
providers: [
{ provide: UserSettingsService, useValue: { get: UserSettings } },
{ provide: LanguageService, useValue: { get: languageSettings } },
]
});
service = TestBed.inject(EnketoPrepopulationDataService);
Expand Down Expand Up @@ -216,17 +221,20 @@ describe('EnketoPrepopulationData service', () => {
});
});

it('binds user details and form content into model', () => {
it('binds user details, user language and form content into model', () => {
const data = { person: { last_name: 'salmon' } };
const user = { name: 'geoff' };
UserSettings.resolves(user);
languageSettings.resolves('en');
return service
.get(editPersonForm, data)
.then((actual) => {
const xml = $($.parseXML(actual));
expect(xml.find('inputs > user > name')[0].innerHTML).to.equal(user.name);
expect(xml.find('inputs > user > language')[0].innerHTML).to.equal('en');
expect(xml.find('data > person > last_name')[0].innerHTML).to.equal(data.person.last_name);
expect(UserSettings.callCount).to.equal(1);
expect(languageSettings.callCount).to.equal(1);
});
});

Expand Down

0 comments on commit 3c2f599

Please sign in to comment.