Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.

Commit 8efad1d

Browse files
minor refactoring of formatters + tests (#19)
1 parent bfbb158 commit 8efad1d

9 files changed

Lines changed: 516 additions & 46 deletions

File tree

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ module.exports = {
1010
maxWorkers: 2,
1111
moduleDirectories: ['node_modules'],
1212
moduleFileExtensions: ['js', 'json'],
13-
testEnvironment: 'node'
13+
testEnvironment: 'node',
14+
testMatch: ['**/__tests__/**/*.test.js', '**/?(*.)+(spec|test).js']
1415
}

src/__tests__/intl.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { getActionTranslations } = require('../intl')
2+
3+
describe('intl', () => {
4+
describe('getActionTranslations', () => {
5+
test('returns dict. of action name and tip according to current language', () => {
6+
expect(getActionTranslations('addToFavorites', 'en')).toEqual({
7+
action: '/add to favorites',
8+
actionTip: 'to access favorites list enter "."'
9+
})
10+
11+
expect(getActionTranslations('addToFavorites', 'ru')).toEqual({
12+
action: '/добавить в избранное',
13+
actionTip: 'получить доступ к списку избранного можно набрав "."'
14+
})
15+
16+
expect(getActionTranslations('save', 'en')).toEqual({
17+
action: '/save',
18+
actionTip: 'save updated translation'
19+
})
20+
21+
expect(getActionTranslations('save', 'ru')).toEqual({
22+
action: '/сохранить',
23+
actionTip: 'сохранит обновленный перевод'
24+
})
25+
})
26+
})
27+
})

src/__tests__/output.test.js

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
const { getTranslationSource } = require('../translation-direction')
2+
const {
3+
formatMainTranslation,
4+
formatOtherTranslations,
5+
formatAutoCorrection,
6+
formatLastSearch,
7+
addToFavoritesAction
8+
} = require('../output')
9+
10+
const getStrLanguage = getTranslationSource
11+
12+
describe('output', () => {
13+
describe('formatMainTranslation', () => {
14+
test('prepares raw translation to alfy-output in format {title, subtitle}', () => {
15+
// originalInput = 'castle'
16+
const translation = 'замок'
17+
const pronunciation = 'kasəl'
18+
const targetLang = 'en'
19+
20+
const mainTranslation = formatMainTranslation(
21+
translation,
22+
pronunciation,
23+
targetLang
24+
)
25+
26+
expect(mainTranslation).toEqual({
27+
title: 'замок [kasəl]',
28+
subtitle: 'best fit translation'
29+
})
30+
})
31+
32+
describe('pronunciation', () => {
33+
test('adds "pronunciation" next to translation if there is one', () => {
34+
// originalInput = 'indeed'
35+
const translation = 'верно'
36+
const pronunciation = 'inˈdēd'
37+
const targetLang = 'en'
38+
39+
const mainTranslation = formatMainTranslation(
40+
translation,
41+
pronunciation,
42+
targetLang
43+
)
44+
45+
expect(mainTranslation).toEqual({
46+
title: 'верно [inˈdēd]',
47+
subtitle: 'best fit translation'
48+
})
49+
})
50+
51+
test('returns only translation, if no pronunciation found', () => {
52+
// originalInput = 'indeed'
53+
const translation = 'верно'
54+
const pronunciation = undefined
55+
const targetLang = 'en'
56+
57+
const mainTranslation = formatMainTranslation(
58+
translation,
59+
pronunciation,
60+
targetLang
61+
)
62+
63+
expect(mainTranslation).toEqual({
64+
title: 'верно',
65+
subtitle: 'best fit translation'
66+
})
67+
})
68+
})
69+
70+
describe('shows translation and tip in subtitle in "targetLang" and pronunciation in "sourceLang"', () => {
71+
test('for en-ru direction', () => {
72+
// originalInput = 'dwell'
73+
const translation = 'жить'
74+
const pronunciation = 'dwell'
75+
const targetLang = 'ru'
76+
77+
const mainTranslation = formatMainTranslation(
78+
translation,
79+
pronunciation,
80+
targetLang
81+
)
82+
const hintLang = getStrLanguage(mainTranslation.subtitle)
83+
84+
expect(mainTranslation).toEqual({
85+
title: 'жить [dwell]',
86+
subtitle: 'найболее подходящий перевод'
87+
})
88+
89+
expect(hintLang).toBe('ru')
90+
})
91+
92+
test('for ru-en direction', () => {
93+
// originalInput = 'обитать'
94+
const translation = 'dwell'
95+
const pronunciation = "obitat'"
96+
const targetLang = 'en'
97+
98+
const mainTranslation = formatMainTranslation(
99+
translation,
100+
pronunciation,
101+
targetLang
102+
)
103+
const hintLang = getStrLanguage(mainTranslation.subtitle)
104+
105+
expect(mainTranslation).toEqual({
106+
title: "dwell [obitat']",
107+
subtitle: 'best fit translation'
108+
})
109+
110+
expect(hintLang).toBe('en')
111+
})
112+
})
113+
})
114+
115+
describe('formatOtherTranslations', () => {
116+
test('prepares other translations to alfy-output format of {title: translation}', () => {
117+
const translations = [
118+
'замок',
119+
'дворец',
120+
'ладья',
121+
'твердыня',
122+
'рокировка',
123+
'убежище'
124+
]
125+
126+
const formattedOtherTranslations = formatOtherTranslations(translations)
127+
128+
expect(formattedOtherTranslations).toEqual([
129+
{
130+
title: 'дворец'
131+
},
132+
{
133+
title: 'ладья'
134+
},
135+
{
136+
title: 'твердыня'
137+
},
138+
{
139+
title: 'рокировка'
140+
},
141+
{
142+
title: 'убежище'
143+
}
144+
])
145+
})
146+
147+
test('not fails, when translations are empty', () => {
148+
expect(formatOtherTranslations()).toEqual([])
149+
150+
expect(formatOtherTranslations([])).toEqual([])
151+
})
152+
})
153+
154+
describe('formatAutoCorrection', () => {
155+
test('prepares auto corrected value to alfy-output format', () => {
156+
// originalInput = 'casle'
157+
const correctedValue = 'castle'
158+
const targetLang = 'ru'
159+
160+
const autoCorrectedItem = formatAutoCorrection(correctedValue, targetLang)
161+
const hintLang = getStrLanguage(autoCorrectedItem.subtitle)
162+
163+
expect(autoCorrectedItem).toEqual({
164+
title: 'castle',
165+
subtitle: 'возможно вы имели ввиду ⤴️',
166+
autocomplete: 'castle',
167+
icon: { path: './icons/question.png' }
168+
})
169+
170+
expect(hintLang).toBe(targetLang)
171+
expect(typeof autoCorrectedItem.icon.path).toBe('string')
172+
})
173+
})
174+
175+
describe('formatLastSearch', () => {
176+
test('prepares last searched input to alfy-output format', () => {
177+
const lastUserInput = 'dog'
178+
const targetLang = 'ru'
179+
180+
const lastSearchItem = formatLastSearch(lastUserInput, targetLang)
181+
const hintLang = getStrLanguage(lastSearchItem.subtitle)
182+
183+
expect(lastSearchItem).toEqual({
184+
title: 'dog',
185+
subtitle: 'предыдущий запрос',
186+
icon: { path: './icons/history.png' }
187+
})
188+
189+
expect(hintLang).toBe(targetLang)
190+
expect(typeof lastSearchItem.icon.path).toBe('string')
191+
})
192+
})
193+
194+
describe('addToFavoritesAction', () => {
195+
test('prepares addToFavs action to alfy-format, with proper arguments', () => {
196+
const userInput = 'castle'
197+
const translation = 'замок'
198+
const otherTranslations = [
199+
'замок',
200+
'дворец',
201+
'ладья',
202+
'твердыня',
203+
'рокировка',
204+
'убежище'
205+
]
206+
const targetLang = 'ru'
207+
208+
const addToFavsItem = addToFavoritesAction(
209+
userInput,
210+
translation,
211+
otherTranslations,
212+
targetLang
213+
)
214+
const titleLang = getStrLanguage(addToFavsItem.title)
215+
const hintLang = getStrLanguage(addToFavsItem.subtitle)
216+
const otherTranslationsInArg = /"translations":"(.*)"/gi.exec(
217+
addToFavsItem.arg
218+
)[1]
219+
console.log('otherTranslationsInArg', otherTranslationsInArg)
220+
221+
expect(addToFavsItem).toEqual({
222+
title: '/добавить в избранное',
223+
subtitle: 'получить доступ к списку избранного можно набрав "."',
224+
icon: { path: './icons/bookmark.png' },
225+
arg:
226+
'{"alfredworkflow":{"variables":{"action":"add","word":"castle","translations":"замок, дворец, ладья, твердыня, рокировка, убежище"}}}'
227+
})
228+
229+
expect(titleLang).toBe(targetLang)
230+
expect(hintLang).toBe(targetLang)
231+
expect(typeof addToFavsItem.icon.path).toBe('string')
232+
expect(otherTranslations.join(', ')).toEqual(otherTranslationsInArg)
233+
})
234+
235+
// TODO: add test cases for 1 otherTranslations and 0 otherTranslations
236+
})
237+
})

0 commit comments

Comments
 (0)