Browse Source

fix(cypress): Really mock the initial state instead of trying to stub a module

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
tags/v28.0.0beta1
Ferdinand Thiessen 9 months ago
parent
commit
650312580f
3 changed files with 53 additions and 41 deletions
  1. 21
    41
      apps/files/src/views/Navigation.cy.ts
  2. 15
    0
      cypress.d.ts
  3. 17
    0
      cypress/support/component.ts

+ 21
- 41
apps/files/src/views/Navigation.cy.ts View File

import * as InitialState from '@nextcloud/initial-state'
import * as L10n from '@nextcloud/l10n'
import FolderSvg from '@mdi/svg/svg/folder.svg' import FolderSvg from '@mdi/svg/svg/folder.svg'
import ShareSvg from '@mdi/svg/svg/share-variant.svg' import ShareSvg from '@mdi/svg/svg/share-variant.svg'
import { createTestingPinia } from '@pinia/testing' import { createTestingPinia } from '@pinia/testing'
const Navigation = new NavigationService() as NavigationService const Navigation = new NavigationService() as NavigationService


before(() => { before(() => {
cy.stub(InitialState, 'loadState')
.returns({
used: 1000 * 1000 * 1000,
quota: -1,
})
cy.mockInitialState('files', 'storageStats', {
used: 1000 * 1000 * 1000,
quota: -1,
})
}) })


after(() => cy.unmockInitialState())

it('renders', () => { it('renders', () => {
cy.mount(NavigationView, { cy.mount(NavigationView, {
propsData: { propsData: {
describe('Quota rendering', () => { describe('Quota rendering', () => {
const Navigation = new NavigationService() const Navigation = new NavigationService()


beforeEach(() => {
// TODO: remove when @nextcloud/l10n 2.0 is released
// https://github.com/nextcloud/nextcloud-l10n/pull/542
cy.stub(L10n, 'translate', (app, text, vars = {}, number) => {
cy.log({ app, text, vars, number })
return text.replace(/%n/g, '' + number).replace(/{([^{}]*)}/g, (match, key) => {
return vars[key]
})
})
})
afterEach(() => cy.unmockInitialState())


it('Unknown quota', () => { it('Unknown quota', () => {
cy.stub(InitialState, 'loadState')
.as('loadStateStats')
.returns(undefined)

cy.mount(NavigationView, { cy.mount(NavigationView, {
propsData: { propsData: {
Navigation, Navigation,
}) })


it('Unlimited quota', () => { it('Unlimited quota', () => {
cy.stub(InitialState, 'loadState')
.as('loadStateStats')
.returns({
used: 1000 * 1000 * 1000,
quota: -1,
})
cy.mockInitialState('files', 'storageStats', {
used: 1000 * 1000 * 1000,
quota: -1,
})


cy.mount(NavigationView, { cy.mount(NavigationView, {
propsData: { propsData: {
}) })


it('Non-reached quota', () => { it('Non-reached quota', () => {
cy.stub(InitialState, 'loadState')
.as('loadStateStats')
.returns({
used: 1000 * 1000 * 1000,
quota: 5 * 1000 * 1000 * 1000,
relative: 20, // percent
})
cy.mockInitialState('files', 'storageStats', {
used: 1000 * 1000 * 1000,
quota: 5 * 1000 * 1000 * 1000,
relative: 20, // percent
})


cy.mount(NavigationView, { cy.mount(NavigationView, {
propsData: { propsData: {
}) })


it('Reached quota', () => { it('Reached quota', () => {
cy.stub(InitialState, 'loadState')
.as('loadStateStats')
.returns({
used: 5 * 1000 * 1000 * 1000,
quota: 1000 * 1000 * 1000,
relative: 500, // percent
})
cy.mockInitialState('files', 'storageStats', {
used: 5 * 1000 * 1000 * 1000,
quota: 1000 * 1000 * 1000,
relative: 500, // percent
})


cy.mount(NavigationView, { cy.mount(NavigationView, {
propsData: { propsData: {

+ 15
- 0
cypress.d.ts View File

namespace Cypress { namespace Cypress {
interface Chainable { interface Chainable {
mount: typeof mount; mount: typeof mount;
/**
* Mock an initial state for component testing
*
* @param app App name of the initial state
* @param key Key of the initial state
* @param value The mocked value of the initial state
*/
mockInitialState: (app: string, key: string, value: any) => void
/**
* Unmock all initial states or one defined by app and key
*
* @param app app name of the inital state
* @param key the key of the the initial state
*/
unmockInitialState: (app?: string, key?: string) => void
} }
} }
} }

+ 17
- 0
cypress/support/component.ts View File

return cy.wrap(instance).as('component') return cy.wrap(instance).as('component')
}) })
}) })

Cypress.Commands.add('mockInitialState', (app: string, key: string, value: any) => {
cy.document().then(($document) => {
const input = $document.createElement('input')
input.setAttribute('type', 'hidden')
input.setAttribute('id', `initial-state-${app}-${key}`)
input.setAttribute('value', btoa(JSON.stringify(value)))
$document.body.appendChild(input)
})
})

Cypress.Commands.add('unmockInitialState', (app?: string, key?: string) => {
cy.document().then(($document) => {
$document.querySelectorAll('body > input[type="hidden"]' + (app ? `[id="initial-state-${app}-${key}"]` : ''))
.forEach((node) => $document.body.removeChild(node))
})
})

Loading…
Cancel
Save