Skip to content

Commit

Permalink
Merge pull request #28 from Rue-pro/e2e-tests-for-note-selectors
Browse files Browse the repository at this point in the history
E2e tests for note selectors
  • Loading branch information
Rue-pro authored May 22, 2024
2 parents 8637dbb + 4a2459a commit ad3b847
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 39 deletions.
246 changes: 240 additions & 6 deletions e2e/example.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,245 @@
import { expect, test } from '@playwright/test'

test('Test for testing playwright functionality, should visit google.com', async ({
page,
}) => {
await page.goto('https://www.google.com/')
import { DICTIONARIES } from '@shared/entities/dictionary/model/dictionaries'
import { IDictionary } from '@shared/entities/dictionary/model/types'
import { TLanguageCode } from '@shared/entities/language'
import { INoteSelectors } from '@shared/entities/note'

const mailText = page.getByText('Gmail')
const CONFIG: Record<
Exclude<TLanguageCode, 'other'>,
Record<
string,
{
wordUrl: string
variants: Record<string, INoteSelectors>
}
>
> = {
en: {
en_CambridgeDictionary: {
wordUrl: 'https://dictionary.cambridge.org/dictionary/english/criterion',
variants: {
us: {
text: 'criterion',
transcription: '/kraɪˈtɪr.i.ən/',
context:
'The Health Service should not be judged by financial criteria alone.',
translation:
'a standard by which you judge, decide about, or deal with something:',
},
uk: {
text: 'criterion',
transcription: '/kraɪˈtɪə.ri.ən/',
context:
'The Health Service should not be judged by financial criteria alone.',
translation:
'a standard by which you judge, decide about, or deal with something:',
},
},
},
en_MerriamWebster: {
wordUrl: 'https://www.merriam-webster.com/dictionary/hedge',
variants: {
common: {
text: 'hedge',
transcription: 'ˈhej',
context:
'pikemen … present a hedge of metal points from which any cavalry would flinch',
translation:
': a fence or boundary formed by a dense row of shrubs or low trees',
},
},
},
en_Collins: {
wordUrl: 'https://www.collinsdictionary.com/dictionary/english/criterion',
variants: {
us: {
text: 'criterion',
transcription: 'kraɪˈtɪriən',
context:
'The most important criterion for entry is that applicants must design and make their own work.',
translation:
'A criterion is a factor on which you judge or decide something.',
},
uk: {
text: 'criterion',
transcription: 'kraɪˈtɪərɪən',
context:
'The most important criterion for entry is that applicants must design and make their own work.',
translation:
'A criterion is a factor on which you judge or decide something.',
},
},
},
en_Wordreference: {
wordUrl: 'https://www.wordreference.com/definition/criterion',
variants: {
us: {
text: 'criterion',
transcription: '/kraɪˈtɪriən/',
context:
'Which criterion is the most important when you grade essays?',
translation: 'a standard by which something can be judged or decided',
},
uk: {
text: 'criterion',
transcription: '/kraɪˈtɪərɪən/',
context:
'Which criterion is the most important when you grade essays?',
translation: 'a standard by which something can be judged or decided',
},
},
},
},
ja: {
ja_Jisho: {
wordUrl: 'https://jisho.org/search/criterion',
variants: {
common: {
text: 'きじゅん',
transcription: '',
context: `ひとつの
じゅうよう
重要な
さいよう
採用
きじゅん
基準に「リーダーシップ」があることは
まちが
間違いないです。`,
translation:
'standard; criterion; norm; benchmark; measure; gauge; basis',
},
},
},
},
'pt-BR': {
'pt-BR_Wordreference': {
wordUrl: 'https://www.wordreference.com/enpt/criterion',
variants: {
common: {
text: `criterion,
plural: criteria n`,
transcription: '',
context: '',
translation: 'critério sm',
},
},
},
},
}

expect(mailText).toBeDefined()
test('Should be test config for every variant of dictionary', () => {
for (const [languageCode, language] of Object.entries(DICTIONARIES)) {
if (languageCode === 'other') continue

for (const dictionary of language['dictionaries']) {
const languageTestConfig = CONFIG[languageCode]

if (!languageTestConfig) {
throw Error(`Can not find test config for language ${languageCode}`)
}

const dictionaryTestConfig = CONFIG[languageCode][dictionary.id]

if (!dictionaryTestConfig) {
throw Error(
`Can not find test config for dictionary ${dictionary.id} and language ${languageCode}`,
)
}

if ('variants' in dictionary) {
for (const variant of dictionary.variants) {
const dictionaryVariantTestConfig =
dictionaryTestConfig.variants[variant.value]

if (!dictionaryVariantTestConfig) {
throw Error(
`Can not find test config for variant ${variant.value} dictionary ${dictionary.id} and language ${languageCode}`,
)
}
}
} else {
if (!dictionaryTestConfig.variants.common) {
throw Error(
`Can not find test config for variant common dictionary ${dictionary.id} and language ${languageCode}`,
)
}
}
}
}
})

for (const [languageCode, language] of Object.entries(DICTIONARIES)) {
if (languageCode === 'other') continue

for (const dictionary of language['dictionaries']) {
if ('variants' in dictionary) {
for (const variant of dictionary.variants) {
noteSelectorsTest({
languageCode: languageCode as TLanguageCode,
dictionary: {
id: dictionary.id,
variant: variant.value,
selectors: variant.selectors,
},
wordUrl: CONFIG[languageCode as TLanguageCode][dictionary.id].wordUrl,
testSelectorsResult:
CONFIG[languageCode as TLanguageCode][dictionary.id].variants[
variant.value
],
})
}
} else {
noteSelectorsTest({
languageCode: languageCode as TLanguageCode,
dictionary: {
id: dictionary.id,
variant: 'common',
selectors: dictionary.selectors,
},
wordUrl: CONFIG[languageCode as TLanguageCode][dictionary.id].wordUrl,
testSelectorsResult:
CONFIG[languageCode as TLanguageCode][dictionary.id].variants.common,
})
}
}
}

function noteSelectorsTest(props: {
languageCode: TLanguageCode
wordUrl: string
dictionary: {
variant: string
id: IDictionary['id']
selectors: INoteSelectors
}
testSelectorsResult: INoteSelectors
}) {
test(`Should return autofilled word for language ${props.languageCode} dictionary ${props.dictionary.id} and variant ${props.dictionary.variant}`, async ({
page,
}) => {
await page.goto(props.wordUrl, { waitUntil: 'domcontentloaded' })

const text = await getText('text')
expect(text).toBe(props.testSelectorsResult.text)

const transcription = await getText('transcription')
expect(transcription).toBe(props.testSelectorsResult.transcription)

const translation = await getText('translation')
expect(translation).toBe(props.testSelectorsResult.translation)

const context = await getText('context')
expect(context).toBe(props.testSelectorsResult.context)

async function getText(selector: string) {
if (!props.dictionary.selectors[selector]) return ''

const selectorLocator = page.locator(props.dictionary.selectors[selector])
const selectorTexts = await selectorLocator.allInnerTexts()

return selectorTexts[0].trim()
}
})
}
2 changes: 0 additions & 2 deletions e2e/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@app/*": ["../src/app/*"],
"@entities/*": ["../src/entities/*"],
"@shared/*": ["../src/shared/*"]
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"dev": "vite dev --config=vite.config.popup.ts",
"build": "tsc && vite build --config=vite.config.popup.ts && vite build --config=vite.config.ankiWeb.ts && vite build --config=vite.config.dictionary.ts && vite build --config=vite.config.background.ts",
Expand Down
41 changes: 12 additions & 29 deletions src/shared/entities/dictionary/model/dictionaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const DICTIONARIES: TDictionaries = {
name: 'Merriam-Webster',
url: 'https://www.merriam-webster.com/dictionary',
selectors: {
text: 'hword',
text: '.hword',
transcription: '.play-pron-v2',
context: '.ex-sent',
translation: '.dtText',
Expand All @@ -56,7 +56,7 @@ export const DICTIONARIES: TDictionaries = {
value: 'us',
selectors: {
text: '.h2_entry .orth',
transcription: '.pron .type-ipa',
transcription: '.american .pron',
context: '.quote',
translation: '.def',
},
Expand All @@ -65,9 +65,9 @@ export const DICTIONARIES: TDictionaries = {
label: 'British',
value: 'uk',
selectors: {
text: '.headword',
transcription: '.uk .pron',
context: '.eg',
text: '.h2_entry .orth',
transcription: '.ced .pron',
context: '.quote',
translation: '.def',
},
},
Expand All @@ -85,7 +85,7 @@ export const DICTIONARIES: TDictionaries = {
selectors: {
text: '.headerWord',
transcription: '.pronRH',
context: '.quote',
context: '.rh_ex',
translation: '.definition',
},
},
Expand All @@ -95,7 +95,7 @@ export const DICTIONARIES: TDictionaries = {
selectors: {
text: '.headerWord',
transcription: '.pronWR',
context: '.eg',
context: '.rh_ex',
translation: '.definition',
},
},
Expand All @@ -113,9 +113,9 @@ export const DICTIONARIES: TDictionaries = {
name: 'Jisho',
url: 'https://jisho.org/',
selectors: {
text: '.furigana .kanji',
transcription: '.meaning-meaning',
context: 'sentence .japanese',
text: '.furigana',
transcription: '',
context: '.sentence .japanese',
translation: '.meaning-meaning',
},
},
Expand All @@ -130,27 +130,10 @@ export const DICTIONARIES: TDictionaries = {
name: 'Wordreference',
url: 'https://www.wordreference.com/enpt/',
selectors: {
text: '.even.FrWrd',
transcription: '',
context: '',
translation: '.even.FrWrd',
},
},
],
},
ko: {
label: 'Korean',
value: 'ko',
dictionaries: [
{
id: 'ko_NaverDictionary',
name: 'Jisho',
url: 'https://korean.dict.naver.com/koendict',
selectors: {
text: '.highlight',
text: '.even .FrWrd',
transcription: '',
context: '',
translation: '.u_word_dic',
translation: '.even .ToWrd',
},
},
],
Expand Down
1 change: 0 additions & 1 deletion src/shared/entities/language/model/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ export const LANGUAGES: ILanguage[] = [
{ label: 'English', value: 'en' },
{ label: 'Japanese', value: 'ja' },
{ label: 'Portuguese', value: 'pt-BR' },
{ label: 'Korean', value: 'ko' },
{ label: 'Other', value: 'other' },
]
2 changes: 1 addition & 1 deletion src/shared/entities/language/model/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const LANGUAGE_CODES = ['en', 'ja', 'pt-BR', 'ko', 'other'] as const
export const LANGUAGE_CODES = ['en', 'ja', 'pt-BR', 'other'] as const

export type TLanguageCode = (typeof LANGUAGE_CODES)[number]

Expand Down

0 comments on commit ad3b847

Please sign in to comment.