Skip to content

Commit

Permalink
Merge pull request #39 from unimal-jp/fix/spec-failed
Browse files Browse the repository at this point in the history
update spec files
  • Loading branch information
akoarum committed Nov 10, 2023
2 parents 0ac0d03 + c036717 commit 850254e
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 41 deletions.
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ module.exports = {
tsconfig: './tsconfig.test.json'
}],
},
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
transformIgnorePatterns: ['/node_modules/(?!(axios)/)'],
testMatch: [
'<rootDir>/src/**/*.spec.{js,ts}',
],
testEnvironmentOptions: {
customExportConditions: ['node', 'node-addons'],
}
}
2 changes: 1 addition & 1 deletion src/components/spearly-content/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<script lang="ts" setup>
import { defineProps, reactive, watch, onBeforeUnmount } from 'vue'
import { useSpearly } from 'src/composables'
import { useSpearly } from '../../composables'
import type { Content, GetContentParams } from '@spearly/sdk-js'
export type Props = {
Expand Down
2 changes: 1 addition & 1 deletion src/composables/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './use-spearly'
export { useSpearly } from './use-spearly'
15 changes: 8 additions & 7 deletions src/spec/components/spearly-content-list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { mount, VueWrapper } from '@vue/test-utils'
import { createUseSpearlyMock } from '../createMock'
import * as composable from '../../composables'
import SpearlyContentList from '../../components/spearly-content-list/index.vue'

const mockResponse = {
Expand All @@ -24,7 +26,13 @@ const mockResponse = {
describe('SpearlyContentList', () => {
let wrapper: VueWrapper
let getListMock = jest.fn().mockReturnValue(Promise.resolve(mockResponse))
jest.mock('../../composables')

jest.spyOn(composable, 'useSpearly').mockReturnValue(
createUseSpearlyMock({
getContentList: getListMock,
})
)
beforeEach(() => {
wrapper = mount(SpearlyContentList, {
props: {
Expand All @@ -35,13 +43,6 @@ describe('SpearlyContentList', () => {
template: '<h1>test</h1>',
},
},
global: {
provide: {
$spearly: {
getList: getListMock,
},
},
},
})
})

Expand Down
33 changes: 11 additions & 22 deletions src/spec/components/spearly-content.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { mount, VueWrapper } from '@vue/test-utils'
import * as composable from '../../composables'
import SpearlyContent from '../../components/spearly-content/index.vue'
import { createUseSpearlyMock } from '../createMock'

describe('SpearlyContent', () => {
let wrapper: VueWrapper
Expand All @@ -13,22 +15,20 @@ describe('SpearlyContent', () => {
const pageViewMock = jest.fn().mockResolvedValue({})

beforeEach(() => {
jest.mock('../../composables')

jest.spyOn(composable, 'useSpearly').mockReturnValue(
createUseSpearlyMock({
getContent: getContentMock,
getContentPreview: getContentPreviewMock,
})
)

wrapper = mount(SpearlyContent, {
props: {
contentTypeId: 'CONTENT_TYPE_ID',
id: 'CONTENT_ID',
},
global: {
provide: {
$spearly: {
getContent: getContentMock,
getContentPreview: getContentPreviewMock,
},
$spearlyAnalytics: {
pageView: pageViewMock,
},
},
},
})
})

Expand All @@ -55,17 +55,6 @@ describe('SpearlyContent', () => {
id: 'CONTENT_ID',
previewToken: 'TOKEN',
},
global: {
provide: {
$spearly: {
getContent: getContentMock,
getContentPreview: getContentPreviewMock,
},
$spearlyAnalytics: {
pageView: pageViewMock,
},
},
},
})
expect(getContentPreviewMock).toHaveBeenCalledWith('CONTENT_TYPE_ID', 'CONTENT_ID', 'TOKEN')
})
Expand Down
21 changes: 12 additions & 9 deletions src/spec/components/spearly-form.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { mount, flushPromises, VueWrapper } from '@vue/test-utils'
import * as composable from '../../composables'
import { createUseSpearlyMock } from '../createMock'
import SpearlyForm from '../../components/spearly-form/index.vue'

const getFormLatestMockData = {
Expand Down Expand Up @@ -88,16 +90,17 @@ const wrapperFactory = (
) => {
const options: any = {
props,
global: {
provide: {
$spearly: {
getFormLatest: getFormLatestMock,
postFormAnswers: postFormAnswersMock,
},
},
},
}

jest.mock('../../composables')

jest.spyOn(composable, 'useSpearly').mockReturnValue(
createUseSpearlyMock({
getFormLatest: getFormLatestMock,
postFormAnswer: postFormAnswersMock,
})
)

if (defaultSlots) options.slots = { default: defaultSlots }

const wrapper = mount(SpearlyForm, options)
Expand Down Expand Up @@ -358,7 +361,7 @@ describe('SpearlyForm', () => {
})

it('postFormAnswers is called when clicked on the confirm screen', () => {
expect(postFormAnswersMock).toHaveBeenCalledWith(1, {
expect(postFormAnswersMock).toHaveBeenCalledWith({
_spearly_gotcha: '',
confirmation_email: 'test@example.com',
name: 'example name',
Expand Down
26 changes: 26 additions & 0 deletions src/spec/createMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,29 @@ export const createFormAnswerResponse = (publicUid = 'FORM_ID') => ({
},
createdAt: new Date('2021-08-01'),
})

type UseSpearlyMockParams = {
getContent?: jest.Mock
getContentList?: jest.Mock
getContentPreview?: jest.Mock
getFormLatest?: jest.Mock
postFormAnswer?: jest.Mock
pageView?: jest.Mock
conversion?: jest.Mock
}

export const createUseSpearlyMock = (mocks: UseSpearlyMockParams = {}) => {
return Object.assign(
{},
{
getContent: jest.fn().mockResolvedValue(createContentMock),
getContentList: jest.fn().mockResolvedValue(createContentListMock),
getContentPreview: jest.fn().mockResolvedValue(createContentMock),
getFormLatest: jest.fn().mockResolvedValue(createFormMock),
postFormAnswer: jest.fn().mockResolvedValue(createFormAnswerResponse),
pageView: jest.fn().mockResolvedValue(undefined),
conversion: jest.fn().mockResolvedValue(undefined),
},
mocks
)
}
2 changes: 1 addition & 1 deletion tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@types/node",
"@types/jest",
"jest"
]
],
},
"exclude": ["node_modules"]
}

0 comments on commit 850254e

Please sign in to comment.