Skip to content

Commit

Permalink
fix: properly tear down fake timers
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Jan 29, 2024
1 parent 05aad98 commit c15d122
Show file tree
Hide file tree
Showing 19 changed files with 83 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import OcNotificationMessage from './OcNotificationMessage.vue'
import { defaultPlugins, mount } from 'web-test-helpers'
vi.useFakeTimers()

describe('OcNotificationMessage', () => {
beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

describe('title prop', () => {
it('should set notification message title', () => {
const wrapper = getWrapper()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { useIsVisible } from './index'
import { mount } from 'web-test-helpers'

const mockIntersectionObserver = () => {
vi.useFakeTimers()

const enable = () => {
const mock = {
observe: vi.fn(),
Expand Down Expand Up @@ -48,6 +46,14 @@ const createWrapper = (options = {}) =>
})

describe('useIsVisible', () => {
beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

const { enable: enableIntersectionObserver, disable: disableIntersectionObserver } =
mockIntersectionObserver()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ import {
getComposableWrapper
} from 'web-test-helpers'

vi.useFakeTimers()

describe('resetLogo', () => {
beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

describe('handler', () => {
it('should show message on request success', () => {
getWrapper({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ import {
getComposableWrapper
} from 'web-test-helpers'

vi.useFakeTimers()

describe('uploadImage', () => {
beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

describe('method "uploadImage"', () => {
it('should show message on request success', () => {
getWrapper({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { defaultPlugins, mount } from 'web-test-helpers'
import PrivateLinkItem from 'web-app-files/src/components/SideBar/PrivateLinkItem.vue'
import { useMessages } from '@ownclouders/web-pkg'

vi.useFakeTimers()

const folder = mock<Resource>({
type: 'folder',
owner: {
Expand All @@ -19,6 +17,14 @@ const folder = mock<Resource>({
})

describe('PrivateLinkItem', () => {
beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

it('should render a button', () => {
const { wrapper } = getWrapper()
expect(wrapper.html()).toMatchSnapshot()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import { useMessages } from '@ownclouders/web-pkg'
import NameAndCopy from 'web-app-files/src/components/SideBar/Shares/Links/NameAndCopy.vue'
import { defaultPlugins, mount } from 'web-test-helpers'

vi.useFakeTimers()

const exampleLink = {
name: 'Example link',
url: 'https://some-url.com/abc'
}

describe('NameAndCopy', () => {
beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

// ignore tippy warning
vi.spyOn(console, 'warn').mockImplementation(undefined)
it('should show link info component including a copy-to-clipboard button', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { accentuateItem } from '../../../../src/helpers/ui/filesList'

describe('accentuateItem', () => {
it('highlights given data-item-id', () => {
beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

it('highlights given data-item-id', () => {
document.body.innerHTML =
'<table><tbody><tr data-item-id="1"><td>1</td></tr><tr data-item-id="2"><td>2</td></tr></tbody></table>'
const trs = document.getElementsByTagName('tr')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import { CapabilityStore } from '../../../src/composables/piniaStores'
vi.mock('../../../src/composables/embedMode')
vi.mock('../../../src/composables/passwordPolicyService')
vi.mock('../../../src/helpers/share/link', async (importOriginal) => ({
...(await (importOriginal() as any)),
...(await importOriginal<any>()),
getDefaultLinkPermissions: vi.fn()
}))
vi.mock('../../../src/composables/links', async (importOriginal) => ({
...(await (importOriginal() as any)),
...(await importOriginal<any>()),
useCreateLink: vi.fn()
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Resource } from '@ownclouders/web-client'
import { mock } from 'vitest-mock-extended'

vi.mock('../../../../src/composables/viewMode', async (importOriginal) => ({
...(await (importOriginal() as any)),
...(await importOriginal<any>()),
useTileSize: vi.fn().mockReturnValue({
tileSizePixels: 100
})
Expand Down
2 changes: 1 addition & 1 deletion packages/web-pkg/tests/unit/components/ViewOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { FolderView } from '../../../src'

vi.mock('../../../src/composables/router', async (importOriginal) => ({
...(await (importOriginal() as any)),
...(await importOriginal<any>()),
useRouteQueryPersisted: vi.fn(),
useRouteQuery: vi.fn()
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ vi.mock('../../../../../src/composables/clipboard', () => ({
}))

vi.mock('@ownclouders/web-client/src/helpers/share', async (importOriginal) => ({
...(await (importOriginal() as any)),
...(await importOriginal<any>()),
buildShare: vi.fn()
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useCreateLink, useDefaultLinkPermissions } from '../../../../../src/com
import { SharePermissionBit } from '@ownclouders/web-client/src/helpers'

vi.mock('../../../../../src/composables/links', async (importOriginal) => ({
...(await (importOriginal() as any)),
...(await importOriginal<any>()),
useCreateLink: vi.fn(),
useDefaultLinkPermissions: vi.fn()
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Resource } from '@ownclouders/web-client'
import { GetFileContentsResponse } from '@ownclouders/web-client/src/webdav/getFileContents'

vi.mock('../../../../../src/composables/router', async (importOriginal) => ({
...(await (importOriginal() as any)),
...(await importOriginal<any>()),
useRoute: vi.fn()
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Resource, SpaceResource } from '@ownclouders/web-client'
import { useFileActions, Action, useOpenWithDefaultApp } from '../../../../src/composables'

vi.mock('../../../../src/composables/actions/files', async (importOriginal) => ({
...(await (importOriginal() as any)),
...(await importOriginal<any>()),
useFileActions: vi.fn()
}))

Expand Down
9 changes: 8 additions & 1 deletion packages/web-pkg/tests/unit/helpers/cache/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ const newCache = <T>(vs: T[], ttl?: number, capacity?: number): Cache<number, T>
}

describe('Cache', () => {
beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

it('can set and get entries', () => {
const cacheValues: number[] = [1, 2, 3, 4]
const cache = newCache(cacheValues)
Expand Down Expand Up @@ -42,7 +50,6 @@ describe('Cache', () => {
})

it('can handle ttl', () => {
vi.useFakeTimers()
const cacheValues: number[] = []
const cache = newCache(cacheValues, 50)

Expand Down
3 changes: 1 addition & 2 deletions packages/web-pkg/tests/unit/helpers/datetime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ describe('datetime helper', () => {
})
})
describe('formatDateFromJSDate', () => {
// FIXME
it.skip('should give correct output', () => {
it('should give correct output', () => {
expect(formatDateFromJSDate(new Date('2010-10-22T21:38:00'), language, dateFormat)).toBe(
'Oct 22, 2010, 9:38 PM'
)
Expand Down
5 changes: 4 additions & 1 deletion packages/web-pkg/tests/unit/helpers/share/link.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { createTestingPinia } from 'web-test-helpers'
import { useCapabilityStore } from '../../../../src/composables/piniaStores'

describe('getDefaultLinkPermissions', () => {
afterEach(() => {
vi.useRealTimers()
})

it('returns internal if user is not allowed to create public links', () => {
const permissions = getDefaultLinkPermissions({
ability: mock<Ability>({ can: () => false }),
Expand All @@ -29,7 +33,6 @@ describe('getDefaultLinkPermissions', () => {
describe('getExpirationRules', () => {
it('correctly computes rules based on the "expire_date"-capability', () => {
vi.useFakeTimers().setSystemTime(new Date('2000-01-01'))

createTestingPinia()
const capabilityStore = useCapabilityStore()
const capabilities = mock<PublicExpirationCapability>({ enforced: true, days: '10' })
Expand Down
5 changes: 5 additions & 0 deletions packages/web-pkg/tests/unit/services/loadingService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ describe('LoadingService', () => {
beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

it('adds a task and sets it inactive initially', () => {
const service = new LoadingService()
const action = new Promise((resolve) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/web-runtime/tests/unit/components/Avatar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Avatar component', () => {
window.URL.createObjectURL = vi.fn()

it('should set user when the component is mounted', () => {
const spySetUser = vi.spyOn((Avatar as any).methods, 'setUser')
const spySetUser = vi.spyOn(Avatar.methods, 'setUser')
getShallowWrapper()
expect(spySetUser).toHaveBeenCalledTimes(1)
expect(spySetUser).toHaveBeenCalledWith(propsData.userid)
Expand Down

0 comments on commit c15d122

Please sign in to comment.