-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge pull request #41 from mnenie/dev
test: add tests for settings feature, refactoring
- Loading branch information
Showing
16 changed files
with
262 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/features/settings/lang-switcher/ui/__tests__/LanguageSwitcher.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { mount } from '@vue/test-utils'; | ||
import { describe, it, expect, vi } from 'vitest'; | ||
import { nextTick, ref, shallowReactive } from 'vue'; | ||
import '@/shared/lib/vitest-utils/cookiesI18n-mock'; | ||
import i18n from '@/shared/lib/i18n'; | ||
import LanguageSwitcher from '../LanguageSwitcher.vue'; | ||
import { useLanguage } from '@/shared/lib/composables'; | ||
import { useI18n } from 'vue-i18n'; | ||
|
||
vi.mock('vue-i18n', async (importOriginal) => { | ||
const actual = await importOriginal(); | ||
return { | ||
...(actual as unknown as object), | ||
useI18n: () => ({ | ||
locale: ref('en-US') | ||
}) | ||
}; | ||
}); | ||
|
||
describe('tests for LanguageSwitcher.vue', () => { | ||
const wrapper = mount(LanguageSwitcher, { | ||
global: { | ||
plugins: [i18n] | ||
} | ||
}); | ||
|
||
it('should render correctly', () => { | ||
expect(wrapper.html()).toMatchSnapshot(); | ||
}); | ||
it('should render subcomponents', () => { | ||
expect(wrapper.findComponent({ name: 'UiSelect' }).exists()).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('useLanguage composable tests', () => { | ||
// mocks | ||
const options = shallowReactive([ | ||
{ name: 'English', value: 'en-US' }, | ||
{ name: 'Русский', value: 'ru-RU' }, | ||
{ name: '简体中文', value: 'zh-CN' } | ||
]); | ||
|
||
it('should correctly sync language with `locale`', async () => { | ||
const { locale } = useI18n(); | ||
const language = ref(''); | ||
|
||
useLanguage(options, language); | ||
|
||
expect(language.value).toBe('English'); | ||
|
||
locale.value = 'ru-RU'; | ||
await nextTick(); | ||
expect(locale.value).toBe('ru-RU'); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
src/features/settings/lang-switcher/ui/__tests__/__snapshots__/LanguageSwitcher.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`tests for LanguageSwitcher.vue > should render correctly 1`] = ` | ||
"<div class="custom_select"> | ||
<div class="selected"><span class="text-sm">English</span><svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="var(--zinc-400)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-chevrons-up-down-icon"> | ||
<path d="m7 15 5 5 5-5"></path> | ||
<path d="m7 9 5-5 5 5"></path> | ||
</svg></div> | ||
<transition-stub name="dropdown" appear="false" persisted="false" css="true"> | ||
<!--v-if--> | ||
</transition-stub> | ||
</div>" | ||
`; | ||
|
||
exports[`tests for LanguageSwitcher.vue > should render subcomponents 1`] = ` | ||
"<div class="custom_select"> | ||
<div class="selected"><span class="text-sm">English</span><svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="var(--zinc-400)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-chevrons-up-down-icon"> | ||
<path d="m7 15 5 5 5-5"></path> | ||
<path d="m7 9 5-5 5 5"></path> | ||
</svg></div> | ||
<transition-stub name="dropdown" appear="false" persisted="false" css="true"> | ||
<!--v-if--> | ||
</transition-stub> | ||
</div>" | ||
`; |
4 changes: 2 additions & 2 deletions
4
src/features/settings/theme-switcher/lib/composables/useTheme.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/features/settings/theme-switcher/ui/__tests__/ThemeBlocks.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { shallowMount } from '@vue/test-utils'; | ||
import LightBlock from '../LightBlock.vue'; | ||
import DarkBlock from '../DarkBlock.vue'; | ||
import SystemBlock from '../SystemBlock.vue'; | ||
|
||
describe('tests for theme blocks -> radio buttons', () => { | ||
it('should render correctly LightBlock.vue', () => { | ||
const wrapper = shallowMount(LightBlock); | ||
expect(wrapper.html()); | ||
}); | ||
it('should render correctly DarkBlock.vue', () => { | ||
const wrapper = shallowMount(DarkBlock); | ||
expect(wrapper.html()); | ||
}); | ||
it('should render correctly SystemBlock.vue', () => { | ||
const wrapper = shallowMount(SystemBlock); | ||
expect(wrapper.html()); | ||
}); | ||
}); |
67 changes: 67 additions & 0 deletions
67
src/features/settings/theme-switcher/ui/__tests__/ThemeSwitcher.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { describe, it, expect, vi } from 'vitest'; | ||
import { mount } from '@vue/test-utils'; | ||
import { ref, shallowRef } from 'vue'; | ||
import '@/shared/lib/vitest-utils/cookiesI18n-mock'; | ||
import useTheme from '../../lib/composables/useTheme'; | ||
import ThemeSwitcher from '../ThemeSwitcher.vue'; | ||
import i18n from '@/shared/lib/i18n'; | ||
|
||
vi.mock('@vueuse/core', () => ({ | ||
useColorMode: vi.fn(() => ({ | ||
store: ref('auto') | ||
})) | ||
})); | ||
|
||
describe('tests for ThemeSwitcher.vue', () => { | ||
const wrapper = mount(ThemeSwitcher, { | ||
global: { | ||
plugins: [i18n], | ||
mocks: { | ||
t: (key: string) => { | ||
const translations: Record<string, string> = { | ||
'settings.theme.btn': 'change theme', | ||
'settings.theme.variants.light': 'light', | ||
'settings.theme.variants.dark': 'dark', | ||
'settings.theme.variants.auto': 'system' | ||
}; | ||
return translations[key]; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
it('should render correctly', () => { | ||
expect(wrapper.html()).toMatchSnapshot(); | ||
}); | ||
it('should render subcomponents', () => { | ||
// TODO: change `findComponent` selector -> name in another tests | ||
// So the problem with component import is solved ✅ | ||
expect(wrapper.findComponent({ name: 'UiRadioGroupContainer' }).exists()).toBe(true); | ||
expect(wrapper.findComponent({ name: 'UiButton' }).exists()).toBe(true); | ||
}); | ||
|
||
it('computed `themeAbout` is working correctly', () => { | ||
//@ts-expect-error PublicInstance | ||
const themeAbout = wrapper.vm.themeAbout; | ||
expect(themeAbout('light')).toBe('Light'); | ||
expect(themeAbout('dark')).toBe('Dark'); | ||
expect(themeAbout('auto')).toBe('System'); | ||
}); | ||
}); | ||
|
||
describe('useTheme composable tests', () => { | ||
it('should change active theme correctly', () => { | ||
const themeBlocks = shallowRef([ | ||
{ id: 0, active: true, value: 'light' }, | ||
{ id: 1, active: false, value: 'dark' }, | ||
{ id: 2, active: false, value: 'auto' } | ||
]); | ||
|
||
const { changeActiveTheme } = useTheme(themeBlocks as unknown as any); | ||
|
||
changeActiveTheme(1); | ||
expect(themeBlocks.value[0].active).toBe(false); | ||
expect(themeBlocks.value[1].active).toBe(true); | ||
expect(themeBlocks.value[2].active).toBe(false); | ||
}); | ||
}); |
62 changes: 62 additions & 0 deletions
62
src/features/settings/theme-switcher/ui/__tests__/__snapshots__/ThemeSwitcher.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`tests for ThemeSwitcher.vue > should render correctly 1`] = ` | ||
"<form class="theme_form"> | ||
<div class="group"> | ||
<div class="block"> | ||
<div class="main_container"> | ||
<div class="main_content"> | ||
<div class="item"> | ||
<div class="item_line short"></div> | ||
<div class="item_line long"></div> | ||
</div> | ||
<div class="item flex"> | ||
<div class="item_circle"></div> | ||
<div class="item_line long"></div> | ||
</div> | ||
<div class="item flex"> | ||
<div class="item_circle"></div> | ||
<div class="item_line long"></div> | ||
</div> | ||
</div> | ||
</div><span class="text-xs">Light</span> | ||
</div> | ||
<div class="block"> | ||
<div class="main_container"> | ||
<div class="main_content"> | ||
<div class="item"> | ||
<div class="item_line short"></div> | ||
<div class="item_line long"></div> | ||
</div> | ||
<div class="item flex"> | ||
<div class="item_circle"></div> | ||
<div class="item_line long"></div> | ||
</div> | ||
<div class="item flex"> | ||
<div class="item_circle"></div> | ||
<div class="item_line long"></div> | ||
</div> | ||
</div> | ||
</div><span class="text-xs">Dark</span> | ||
</div> | ||
<div class="block"> | ||
<div class="main_container active"> | ||
<div class="main_content"> | ||
<div class="item"> | ||
<div class="item_line short"></div> | ||
<div class="item_line long"></div> | ||
</div> | ||
<div class="item flex"> | ||
<div class="item_circle"></div> | ||
<div class="item_line long"></div> | ||
</div> | ||
<div class="item flex"> | ||
<div class="item_circle"></div> | ||
<div class="item_line long"></div> | ||
</div> | ||
</div> | ||
</div><span class="text-xs">System</span> | ||
</div> | ||
</div><button class="text-sm button outline md" type="submit">change theme</button> | ||
</form>" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { vi } from 'vitest'; | ||
|
||
vi.mock('@vueuse/integrations/useCookies', () => { | ||
return { | ||
useCookies: () => ({ | ||
get(key: string) { | ||
return key === 'i18n' ? 'en-US' : undefined; | ||
} | ||
}) | ||
}; | ||
}); |
15 changes: 3 additions & 12 deletions
15
src/widgets/layout/ui/footer/__tests__/FooterWelcome.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 2 additions & 11 deletions
13
src/widgets/layout/ui/header/__tests__/HeaderWelcome.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.