Skip to content

Commit

Permalink
Add tests - stash
Browse files Browse the repository at this point in the history
  • Loading branch information
kvestus committed Feb 13, 2024
1 parent 759fdd8 commit 60b82f0
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { storeToRefs } from 'pinia'
import { useUserStore } from '@/store/modules/user'
import { EPageName } from '@/enums/EPageName'
import { EAuthenticateModalType } from '@/modules/landing/modules/authenticate/components/AuthenticateModal/EAuthenticateModalType'
// import AuthenticateModal from '@/modules/landing/modules/authenticate/components/AuthenticateModal/AuthenticateModal.vue'
import AuthenticateModal from '@/modules/landing/modules/authenticate/components/AuthenticateModal/AuthenticateModal.vue'
const { isLoggedIn } = storeToRefs(useUserStore())
Expand Down Expand Up @@ -42,9 +42,9 @@ const showAuthenticateModal = (type: EAuthenticateModalType) => {
</n-button>
</n-space>
</div>
<!-- <AuthenticateModal-->
<!-- v-model:show="isNeededToShowAuthenticateModal"-->
<!-- :type="authenticateModalType"/>-->
<AuthenticateModal
v-model:show="isNeededToShowAuthenticateModal"
:type="authenticateModalType"/>
</template>

<style scoped lang="scss">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts" setup>
import { useNotification } from 'naive-ui'
import { reactive } from 'vue'
import { EPageName } from '@/enums/EPageName'
import { type EAuthenticationProvider } from '@/services/authentication/EAuthenticationProvider'
import AuthenticateModalProviders from '@/modules/landing/modules/authenticate/components/AuthenticateModal/components/AuthenticateModalProviders/AuthenticateModalProviders.vue'
Expand Down Expand Up @@ -86,6 +87,7 @@ const action = computed(() => {
<n-form-item-row :label="$t('email')">
<n-input
v-model:value="form.email"
type="email"
:placeholder="$t('email')"/>
</n-form-item-row>
<n-form-item-row :label="$t('password')">
Expand Down
15 changes: 15 additions & 0 deletions src/plugins/__mocks__/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { App } from 'vue'
import type { Router } from 'vue-router'
import { BASE_INTERFACE_LANGUAGE, BASE_INTERFACE_LANGUAGE_NAME } from '@/const/BaseInterfaceLanguage'


export async function setupI18n(router: Router, app: App) {}

export const useI18n = () => {
return {
t: (tKey: string) => tKey,
locale: BASE_INTERFACE_LANGUAGE_NAME,
availableLocales: [BASE_INTERFACE_LANGUAGE],
setLocaleMessage: () => {},
}
}
1 change: 0 additions & 1 deletion src/plugins/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export async function setupI18n(router: Router, app: App) {
const interfaceLanguage = router.currentRoute.value.params.lang as string ?? BASE_INTERFACE_LANGUAGE_NAME
const { translationCollection } = useDbStore()
const translations = await translationCollection.get(interfaceLanguage)
console.log(translations, 'translations')

const i18n = createI18n({
locale: interfaceLanguage,
Expand Down
11 changes: 8 additions & 3 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ export const routes: RouteRecordRaw[] = [
},
]

export async function setupRouter(app: App<Element>, test = false) {
const router = createRouter({
history: test ? createMemoryHistory() : createWebHistory(),
export const createAppRouter = (test = false) => {
return createRouter({
// history: test ? createMemoryHistory() : createWebHistory(),
history: createWebHistory(),
routes,
})
}

export async function setupRouter(app: App<Element>, test = false) {
const router = createAppRouter(test)

app.use(router)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { IAuthentication } from '@/services/authentication/common/IAuthentication'

export class FirebaseAuthentication implements IAuthentication {
private static instance: FirebaseAuthentication

public static getInstance(): IAuthentication {
if (!FirebaseAuthentication.instance) {
FirebaseAuthentication.instance = new FirebaseAuthentication()
}

return FirebaseAuthentication.instance
}

public createUserWithEmailAndPassword = async () => {}

public signInWithEmailAndPassword = async () => {}

private signInWithAuthProvider = async () => {}

public signInWithProvider = async () => {}

public signOut = async () => {}
}
4 changes: 2 additions & 2 deletions src/services/configuration/firebase/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export class Config implements IConfigService {

public getBaseConfig() {
return {
languages: [],
languages: {},
languagesAvailableForLearning: [],
interfaceLanguages: [],
} as IConfig
} satisfies IConfig
}
}
35 changes: 35 additions & 0 deletions src/services/configuration/firebase/__mocks__/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { IConfig } from '@/services/configuration/common/IConfig'
import type { IConfigService } from '@/services/configuration/common/IConfigService'
import { BASE_INTERFACE_LANGUAGE, BASE_INTERFACE_LANGUAGE_NAME } from '@/const/BaseInterfaceLanguage'

export class Config implements IConfigService {
private static instance: Config

public static getInstance(): IConfigService {
if (!Config.instance) {
Config.instance = new Config()
}

return Config.instance
}

public async setup(): Promise<void> {}

public getConfig(): IConfig {
return {
languages: {
[BASE_INTERFACE_LANGUAGE]: BASE_INTERFACE_LANGUAGE_NAME,
},
languagesAvailableForLearning: [BASE_INTERFACE_LANGUAGE],
interfaceLanguages: [BASE_INTERFACE_LANGUAGE],
} satisfies IConfig
}

public getBaseConfig() {
return {
languages: {},
languagesAvailableForLearning: [],
interfaceLanguages: [],
} satisfies IConfig
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export class TranslationsFirestoreCollection extends BaseFirestoreCollection<Tra

public get = async (languageName: string): Promise<TranslationsCollectionItems | undefined> => {
const item = await this._collection.doc(languageName).get()
console.log(item.data(), 'item.data()')

return item.exists ? item.data() as TranslationsCollectionItems : undefined
}
Expand Down
61 changes: 61 additions & 0 deletions tests/common/useMocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { beforeEach, vi } from 'vitest'
import type { Router } from 'vue-router'
import { createAppRouter } from '@/router'
import { BASE_INTERFACE_LANGUAGE_NAME } from '@/const/BaseInterfaceLanguage'

// type FakeFirestore = { onSnapshot(this: void): void }
// vi.mock('firebase/firestore', async (getModule) => {
// const original: FakeFirestore = await getModule()
//
// return {
// ...original,
// onSnapshot: vi.fn().mockImplementation(original.onSnapshot),
// }
// })

export const useMocks = () => {
const mockServices = () => {
vi.mock('@/services/authentication/firebase/FirebaseAuthentication')
vi.mock('@/services/dbstore/firestore/UserFirestoreCollection')
vi.mock('@/services/dbstore/firestore/TranslationsFirestoreCollection')
vi.mock('@/services/configuration/firebase/Config')
}

const mockI18n = () => {
vi.mock('@/plugins/i18n')
}

const mockNotifications = async () => {
vi.mock('naive-ui', async (importOriginal) => {
return {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
...await importOriginal(),
useNotification: () => ({
create: () => {},
info: () => {},
success: () => {},
warning: () => {},
error: () => {},
open: () => {},
destroyAll: () => {},
}),
}
})
}

const routerBeforeEach = async () => {
const router = createAppRouter()
router.push({ params: { lang: BASE_INTERFACE_LANGUAGE_NAME } })
await router.isReady()

return router
}

return {
mockServices,
mockI18n,
routerBeforeEach,
mockNotifications,
}
}
138 changes: 48 additions & 90 deletions tests/modules/landing/components/LandingHeader.hdom.test.ts
Original file line number Diff line number Diff line change
@@ -1,120 +1,78 @@
import {
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
} from 'vitest'
import { mount } from '@vue/test-utils'
import {
type Router,
createRouter,
createWebHistory,
} from 'vue-router'
import { testApp } from '@@/tests/common/testApp'
import { type Router } from 'vue-router'
import { createTestingPinia } from '@pinia/testing'
import { vi } from 'vitest'
import { useMocks } from '@@/tests/common/useMocks'
import LandingHeader from '@/modules/landing/components/LandingHeader/LandingHeader.vue'
import { routes } from '@/router'
import { BASE_INTERFACE_LANGUAGE_NAME } from '@/const/BaseInterfaceLanguage'
import { useInterfaceLanguageStore } from '@/store/modules/interfaceLanguage'
import { useUserStore } from '@/store/modules/user'

// const useTestRouter = async () => {
// const router = createRouter({
// history: createWebHistory(),
// routes: routes,
// })
//
// router.push({ params: { lang: BASE_INTERFACE_LANGUAGE_NAME } })
// await router.isReady()
//
// return router
// }
import { EAuthenticateModalType } from '@/modules/landing/modules/authenticate/components/AuthenticateModal/EAuthenticateModalType'

// type FakeFirestore = { onSnapshot(this: void): void }
// vi.mock('firebase/firestore', async (getModule) => {
// const original: FakeFirestore = await getModule()
//
// return {
// ...original,
// onSnapshot: vi.fn().mockImplementation(original.onSnapshot),
// }
// })
//
// vi.mock('@/plugins/services', () => {
// return {
// useDbStore: () => ({
// userCollection: undefined,
// translationCollection: undefined,
// }),
// }
// })
//
// vi.mock('@/store/modules/interfaceLanguage', () => {
// return {
// useInterfaceLanguageStore: () => ({
// interfaceLanguageId: 0,
// setInterfaceLanguage: () => {},
// }),
// }
// })
vi.mock('@/store/modules/user')
const { mockServices, mockI18n, routerBeforeEach } = useMocks()
mockI18n()
mockServices()

describe('modules/landing/components/LandingHeader', () => {
let router: Router
beforeEach(async () => {
router = createRouter({
history: createWebHistory(),
routes: routes,
})


router.push({ params: { lang: BASE_INTERFACE_LANGUAGE_NAME } })
await router.isReady()
router = await routerBeforeEach()
})

const getWrapper = (isLoggedIn: boolean) => {
return mount(LandingHeader, {
global: {
stubs: { AuthenticateModal: true },
plugins: [
router,
createTestingPinia({
createSpy: vi.fn,
initialState: {
user: {
profileData: isLoggedIn ? {} : undefined,
},
},
}),
],
},
})
}

describe('authorized user', () => {
it('template', async () => {
const wrapper = mount(LandingHeader, {
global: {
plugins: [
router,
createTestingPinia({
createSpy: vi.fn,
initialState: {
user: {
isLoggedIn: true,
},
},
}),
],
},
})
const wrapper = getWrapper(true)
expect(wrapper.text()).toContain('go_to_workspace')
})
})

describe('unauthorized user', () => {
it('template', async () => {
const wrapper = mount(LandingHeader, {
global: {
plugins: [
router,
createTestingPinia({
createSpy: vi.fn,
initialState: {
user: {
isLoggedIn: false,
},
},
}),
],
},
})
const wrapper = getWrapper(false)
expect(wrapper.text()).toContain('sign_in')
expect(wrapper.text()).toContain('sign_up')
})

it('openSignInModal', async () => {
const wrapper = getWrapper(false)
await wrapper.find('button').trigger('click')
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const { setupState } = wrapper.getCurrentComponent()
expect(setupState.isNeededToShowAuthenticateModal).toBe(true)
expect(setupState.authenticateModalType).toBe(EAuthenticateModalType.SIGNIN)
})

it('openSignUpModal', async () => {
const wrapper = getWrapper(false)
await wrapper.findAll('button')[1].trigger('click')
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const { setupState } = wrapper.getCurrentComponent()
expect(setupState.isNeededToShowAuthenticateModal).toBe(true)
expect(setupState.authenticateModalType).toBe(EAuthenticateModalType.SIGNUP)
})
})
})
Loading

0 comments on commit 60b82f0

Please sign in to comment.