generated from xqsit94/vite-vue3-ts-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* should properly call POST function * should load integers list before accessing home page * rm unrelevant file * should call search integers with correst params & set integers array * fix useRandomIntegerStore
- Loading branch information
Showing
12 changed files
with
171 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,33 @@ | ||
import { | ||
searchRandomIntegers, | ||
SearchRandomIntegersParams | ||
SearchRandomIntegersParams, | ||
SearchRandomIntegersResponse | ||
} from '@/features/integersList/searchRandomIntegers.ts' | ||
import { ref } from 'vue' | ||
import { defineStore } from 'pinia' | ||
export const useRandomIntegersStore = defineStore('integersList', () => { | ||
const integersArray = ref<Record<number, number>>([]) | ||
|
||
const searchIntegers = async (params: SearchRandomIntegersParams) => { | ||
integersArray.value = groupIntegersByValue(await searchRandomIntegers(params)) | ||
} | ||
export type IntegersStoreDependencies = { | ||
searchRandomIntegers: SearchRandomIntegersResponse | ||
} | ||
export const createRandomIntegersStore = ({ searchRandomIntegers }: IntegersStoreDependencies) => | ||
defineStore('integersList', () => { | ||
const integersArray = ref<Record<number, number>>({}) | ||
|
||
const groupIntegersByValue = (integersArray: string[]) => { | ||
const occurrences: Record<string, number> = {}; | ||
const searchIntegers = async (params: SearchRandomIntegersParams) => { | ||
integersArray.value = groupIntegersByValue(await searchRandomIntegers(params)) | ||
} | ||
|
||
integersArray.forEach((integer) => { | ||
occurrences[integer] = (occurrences[integer] || 0) + 1; | ||
}); | ||
const groupIntegersByValue = (integersArray: string[]) => { | ||
const occurrences: Record<string, number> = {} | ||
|
||
return occurrences; | ||
} | ||
integersArray.forEach((integer) => { | ||
occurrences[integer] = (occurrences[integer] || 0) + 1 | ||
}) | ||
|
||
return { integersArray, searchIntegers } | ||
}) | ||
return occurrences | ||
} | ||
|
||
return { integersArray, searchIntegers } | ||
}) | ||
|
||
export const useRandomIntegersStore = () => createRandomIntegersStore({ searchRandomIntegers })() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,34 @@ | ||
import { describe, expect } from 'vitest' | ||
import { beforeEach, describe, expect, it, vitest } from 'vitest' | ||
import { | ||
createSearchRandomIntegers, | ||
SearchRandomIntegersDependencies, | ||
SearchRandomIntegersParams, SearchRandomIntegersResponse | ||
} from '../../../src/features/integersList/searchRandomIntegers' | ||
import { Mocks } from '../../utils' | ||
|
||
describe('Search Random Integers Feature', () => { | ||
it('should return true', () => { | ||
expect(1 + 1).toBe(2) | ||
let mocks: Mocks<SearchRandomIntegersDependencies> | ||
let searchRandomIntegers: SearchRandomIntegersResponse | ||
beforeEach(() => { | ||
mocks = { httpPost: vitest.fn() } | ||
searchRandomIntegers = createSearchRandomIntegers(mocks) | ||
}) | ||
|
||
it('should properly call POST', async () => { | ||
const params: SearchRandomIntegersParams = { | ||
base: 2, | ||
col: 1, | ||
format: 'plain', | ||
max: 10, | ||
min: 0, | ||
num: 200 | ||
} | ||
|
||
await searchRandomIntegers(params) | ||
|
||
expect(mocks.httpPost).toHaveBeenCalledWith( | ||
`/integers/?num=200&min=0&max=10&col=1&base=2&format=plain`, | ||
null | ||
) | ||
}) | ||
}) |
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,27 @@ | ||
import PageLayout from '@/layouts/page-layout.vue' | ||
import { createTestingPinia } from '@pinia/testing' | ||
import { describe, it, expect, beforeEach, vi } from 'vitest' | ||
|
||
import { shallowMount, VueWrapper } from '@vue/test-utils' | ||
import { setActivePinia } from 'pinia' | ||
import HomePage from '@/pages/home-page.vue' | ||
import { useRandomIntegersStore } from '../../src/stores/useRandomIntegers.store' | ||
|
||
describe('Home page', () => { | ||
let wrapper: VueWrapper | ||
beforeEach(async () => { | ||
setActivePinia(createTestingPinia()) | ||
vi.mocked(useRandomIntegersStore().searchIntegers) | ||
wrapper = shallowMount(HomePage) | ||
}) | ||
|
||
it('should display a page layout', () => { | ||
const pageLayout = wrapper.findComponent(PageLayout) | ||
|
||
expect(pageLayout.exists()).toBe(true) | ||
}) | ||
|
||
it('should load integers list before accessing home page', () => { | ||
expect(useRandomIntegersStore().searchIntegers).toHaveBeenCalled() | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,8 +1,57 @@ | ||
|
||
import { describe, expect } from 'vitest' | ||
import { createPinia, setActivePinia } from 'pinia' | ||
import { beforeEach, describe, expect, it, vitest } from 'vitest' | ||
import { | ||
createRandomIntegersStore, | ||
IntegersStoreDependencies, | ||
useRandomIntegersStore | ||
} from '../../src/stores/useRandomIntegers.store' | ||
import { Mocks } from '../utils' | ||
|
||
describe('Search Random Integers Feature', () => { | ||
it('should return true', () => { | ||
expect(1 + 1).toBe(2) | ||
}) | ||
let mocks: Mocks<IntegersStoreDependencies> | ||
let randomIntegersStore: ReturnType<typeof useRandomIntegersStore> | ||
|
||
beforeEach(() => { | ||
setActivePinia(createPinia()) | ||
mocks = { | ||
searchRandomIntegers: vitest.fn().mockReturnValue(['1']) | ||
} | ||
randomIntegersStore = createRandomIntegersStore(mocks) | ||
}) | ||
|
||
it('should have default value in the store', () => { | ||
expect(randomIntegersStore()).toEqual( | ||
expect.objectContaining({ | ||
integersArray: {} | ||
}) | ||
) | ||
}) | ||
|
||
it('should call search integers with correct params', () => { | ||
const params = { | ||
base: undefined, | ||
col: 0, | ||
format: undefined, | ||
max: 0, | ||
min: 0, | ||
num: 0 | ||
} | ||
randomIntegersStore().searchIntegers(params) | ||
|
||
expect(mocks.searchRandomIntegers).toHaveBeenCalledWith(params) | ||
}) | ||
|
||
it('should set integers array', async () => { | ||
const params = { | ||
base: undefined, | ||
col: 0, | ||
format: undefined, | ||
max: 0, | ||
min: 0, | ||
num: 0 | ||
} | ||
await randomIntegersStore().searchIntegers(params) | ||
|
||
expect(randomIntegersStore().integersArray).toEqual({1: 1}) | ||
}) | ||
}) |
Empty file.
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,7 @@ | ||
import type { Mock } from 'vitest' | ||
|
||
export type Mocks<Type> = Type extends (...arg: infer Params) => infer ReturnType | ||
? Mock<Params, ReturnType> | ||
: Type extends object | ||
? { [key in keyof Type]: Mocks<Type[key]> } | ||
: Type |
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