Skip to content
This repository has been archived by the owner on Mar 1, 2019. It is now read-only.

Commit

Permalink
store: implement LANGUAGE_UPDATE mutation
Browse files Browse the repository at this point in the history
Add the LANGUAGE_UPDATE mutation and use it with dummy data from the
LANGUAGE_INIT action

Bug: T210407
  • Loading branch information
wiese committed Dec 11, 2018
1 parent 79be4fc commit c2629e9
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 6 deletions.
4 changes: 2 additions & 2 deletions dist/wikibase.termbox.main.js

Large diffs are not rendered by default.

22 changes: 21 additions & 1 deletion src/store/language/actions.ts
@@ -1,14 +1,34 @@
import { ActionContext, ActionTree } from 'vuex';
import { LANGUAGE_INIT, ENSURE_AVAILABLE_IN_LANGUAGE } from './actionTypes';
import { LANGUAGE_TRANSLATION_UPDATE } from './mutationTypes';
import {
LANGUAGE_TRANSLATION_UPDATE,
LANGUAGE_UPDATE,
} from './mutationTypes';
import { factory } from '@/common/TermboxFactory';
import LanguageState from '@/store/language/LanguageState';
import LanguageTranslations from '@/datamodel/LanguageTranslations';
import LanguageCollection from '@/datamodel/LanguageCollection';

export const actions: ActionTree<LanguageState, any> = {

[ LANGUAGE_INIT ]( context: ActionContext<LanguageState, any> ): Promise<void> {
// TODO commit dynamic list from upstream source
// TODO same api. how to do only one call?
context.commit( LANGUAGE_UPDATE, {
en: {
code: 'en',
directionality: 'ltr',
},
de: {
code: 'de',
directionality: 'ltr',
},
ar: {
code: 'ar',
directionality: 'rtl',
},
} as LanguageCollection );

return Promise.resolve();
},

Expand Down
1 change: 1 addition & 0 deletions src/store/language/mutationTypes.ts
@@ -1 +1,2 @@
export const LANGUAGE_TRANSLATION_UPDATE: string = 'translationUpdate';
export const LANGUAGE_UPDATE: string = 'languageUpdate';
8 changes: 8 additions & 0 deletions src/store/language/mutations.ts
@@ -1,11 +1,19 @@
import { MutationTree } from 'vuex';
import {
LANGUAGE_TRANSLATION_UPDATE,
LANGUAGE_UPDATE,
} from './mutationTypes';
import LanguageState from '@/store/language/LanguageState';
import LanguageTranslations from '@/datamodel/LanguageTranslations';
import LanguageCollection from '@/datamodel/LanguageCollection';

export const mutations: MutationTree<LanguageState> = {
[ LANGUAGE_UPDATE ]( state: LanguageState, languages: LanguageCollection ): void {
Object.entries( languages ).forEach( ( [ languageKey, localData ] ) => {
state.languages[ languageKey ] = localData;
} );
},

[ LANGUAGE_TRANSLATION_UPDATE ]( state: LanguageState, translations: LanguageTranslations ): void {
Object.entries( translations ).forEach( ( [ languageKey, localTranslations ] ) => {
state.translations[ languageKey ] = localTranslations;
Expand Down
28 changes: 25 additions & 3 deletions tests/unit/store/language/actions.spec.ts
@@ -1,20 +1,42 @@
import { actions } from '@/store/language/actions';
import { LANGUAGE_INIT, ENSURE_AVAILABLE_IN_LANGUAGE } from '@/store/language/actionTypes';
import { LANGUAGE_TRANSLATION_UPDATE } from '@/store/language/mutationTypes';
import {
LANGUAGE_TRANSLATION_UPDATE,
LANGUAGE_UPDATE,
} from '@/store/language/mutationTypes';
import { factory } from '@/common/TermboxFactory';
import LanguageTranslations from '@/datamodel/LanguageTranslations';
import LanguageCollection from '@/datamodel/LanguageCollection';

describe( 'language/actions', () => {
describe( LANGUAGE_INIT, () => {
it( 'does not commit anything yet, just returns a promise', ( done ) => {
it( 'commits a fixed set of languages and returns a resolved promise', ( done ) => {
const languages = {
en: {
code: 'en',
directionality: 'ltr',
},
de: {
code: 'de',
directionality: 'ltr',
},
ar: {
code: 'ar',
directionality: 'rtl',
},
} as LanguageCollection;

const commitMock = jest.fn();
const context = {
commit: commitMock,
};
const action = actions[ LANGUAGE_INIT ] as any; // TODO

action( context ).then( () => {
expect( commitMock ).not.toBeCalled();
expect( commitMock ).toBeCalledWith(
LANGUAGE_UPDATE,
languages,
);
done();
} );
} );
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/store/language/mutations.spec.ts
@@ -1,9 +1,11 @@
import { mutations } from '@/store/language/mutations';
import {
LANGUAGE_TRANSLATION_UPDATE,
LANGUAGE_UPDATE,
} from '@/store/language/mutationTypes';
import LanguageTranslations from '@/datamodel/LanguageTranslations';
import LanguageState from '@/store/language/LanguageState';
import LanguageCollection from '@/datamodel/LanguageCollection';

function newMinimalStore(): LanguageState {
return {
Expand All @@ -13,6 +15,52 @@ function newMinimalStore(): LanguageState {
}

describe( 'language/mutations', () => {
describe( LANGUAGE_UPDATE, () => {
it( 'contains languages after initialization', () => {
const store = newMinimalStore();
const languages = {
de: {
code: 'de',
directionality: 'ltr',
},
en: {
code: 'en',
directionality: 'ltr',
},
} as LanguageCollection;

mutations[ LANGUAGE_UPDATE ]( store, languages );

expect( store.languages.de ).toBe( languages.de );
expect( store.languages.en ).toBe( languages.en );
} );

it( 'appends new languages to pre-existing ones', () => {
const store = newMinimalStore();
const originalDe = {
code: 'de',
directionality: 'ltr',
};
store.languages.de = originalDe;
const languages = {
en: {
code: 'en',
directionality: 'ltr',
},
ar: {
code: 'ar',
directionality: 'rtl',
},
} as LanguageCollection;

mutations[ LANGUAGE_UPDATE ]( store, languages );

expect( store.languages.de ).toBe( originalDe );
expect( store.languages.en ).toBe( languages.en );
expect( store.languages.ar ).toBe( languages.ar );
} );
} );

describe( LANGUAGE_TRANSLATION_UPDATE, () => {
// TODO
/*
Expand Down

0 comments on commit c2629e9

Please sign in to comment.