lang: unit tests in django/await first loading of dict

This commit is contained in:
AdrienLSH 2024-02-13 14:49:38 +01:00
parent b398134101
commit 157383748f
3 changed files with 34 additions and 4 deletions

View File

@ -8,10 +8,10 @@ export default class LanguageManager {
this.currentLang = 'en' this.currentLang = 'en'
this.chosenLang = localStorage.getItem('preferedLanguage') || this.currentLang; this.chosenLang = localStorage.getItem('preferedLanguage') || this.currentLang;
if (this.chosenLang !== this.currentLang && this.availableLanguages.includes(this.chosenLang)) { if (this.chosenLang !== this.currentLang && this.availableLanguages.includes(this.chosenLang)) {
this.translatePage(); this.loading = this.translatePage();
this.currentLang = this.chosenLang; this.currentLang = this.chosenLang;
} else { } else {
this.loadDict(this.chosenLang); this.loading = this.loadDict(this.chosenLang);
} }
document.getElementById('languageDisplay').innerHTML = document.getElementById('languageDisplay').innerHTML =
document.querySelector(`#languageSelector > [value=${this.currentLang}]`)?.innerHTML; document.querySelector(`#languageSelector > [value=${this.currentLang}]`)?.innerHTML;
@ -61,6 +61,10 @@ export default class LanguageManager {
this.dict = await response.json(); this.dict = await response.json();
} }
async waitLoading() {
await this.loading;
}
get(key, defaultTxt) { get(key, defaultTxt) {
if (!this.dict) if (!this.dict)
return defaultTxt; return defaultTxt;

View File

@ -49,7 +49,10 @@ const navigateTo = async (uri) => {
} }
}; };
const reloadView = async _ => renderView(lastView); const reloadView = async _ => {
await lastView?.leavePage();
await renderView(lastView);
}
async function renderView(view) async function renderView(view)
{ {
@ -142,10 +145,12 @@ document.addEventListener("DOMContentLoaded", async () => {
}); });
//Languages //Languages
await lang.waitLoading();
Array.from(document.getElementById('languageSelector').children).forEach(el => { Array.from(document.getElementById('languageSelector').children).forEach(el => {
el.onclick = async _ => { el.onclick = async _ => {
if (await lang.changeLanguage(el.value)) if (await lang.changeLanguage(el.value))
return; return;
console.log(lang);
document.querySelector('#languageSelector > .active')?.classList.remove('active'); document.querySelector('#languageSelector > .active')?.classList.remove('active');
el.classList.add('active'); el.classList.add('active');
}; };

View File

@ -1,3 +1,24 @@
from django.test import TestCase import json
from os import listdir
from django.test import Client, TestCase
from django.contrib.staticfiles import finders
# Create your tests here. # Create your tests here.
class DictionnariesTest(TestCase):
def setUp(self) -> None:
self.client = Client();
self.directory = finders.find('js/lang/');
def test_lang(self):
keys = None
json_files = listdir(self.directory);
for file in json_files:
with open(f'{self.directory}/{file}') as f:
data: dict = json.load(f);
if (keys is None):
keys = set(data.keys());
else:
self.assertEqual(set(data.keys()), keys);