Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[unit-tests] Implemented todo unit tests for public link view #5756

Merged
merged 2 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/web-app-files/src/views/PublicLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<oc-spinner :aria-hidden="true" />
</template>
<template v-else-if="errorMessage">
<h2 class="oc-login-card-title">
<h2 class="oc-login-card-title oc-login-card-error">
<translate>An error occurred while loading the public link</translate>
</h2>
<p class="oc-text-lead">{{ errorMessage }}</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ describe('Pagination', () => {

it('should set provided current page', () => {
const paginationEl = wrapper.find(selectors.filesPagination)
console.log(wrapper.html())
expect(paginationEl.attributes().currentpage).toBe('1')
})
})
Expand Down
181 changes: 163 additions & 18 deletions packages/web-app-files/tests/unit/views/PublicLink.spec.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,179 @@
import PublicLink from '@files/src/views/PublicLink.vue'
import { getStore, localVue } from './views.setup.js'
import { shallowMount, mount } from '@vue/test-utils'

const theme = {
general: { slogan: 'some-slogan' },
logo: 'some-logo',
loginPage: { backgroundImg: 'some-image' }
}

const stubs = {
'oc-spinner': true,
translate: true
}

const route = { meta: { title: 'some page title' }, params: { token: 'some-token' } }

const component = { ...PublicLink, mounted: jest.fn(), created: jest.fn() }

const selectors = {
loginLogo: '.oc-login-logo',
loginCardFooter: '.oc-login-card-footer',
loginCardBody: '.oc-login-card-body',
submitButton: '.oc-login-authorize-button',
errorMessage: '.oc-login-card-error',
publicLinkPasswordRequired: 'form > .oc-login-card-title'
}

const spinnerStub = 'oc-spinner-stub'
const textInputStub = 'oc-text-input-stub'

describe('PublicLink', () => {
describe('theming options', () => {
test.todo('should have the background image set')
test.todo('should display the page title')
test.todo('should display the logo image inside login card')
test.todo('should display the configuration theme general slogan as the login card footer')
const wrapper = getWrapper()

it('should have the background image and page-title set', () => {
expect(wrapper).toMatchSnapshot()
})
it('should display the logo image inside login card', () => {
const logo = wrapper.find(selectors.loginLogo)

expect(logo).toMatchSnapshot()
})
it('should display the configuration theme general slogan as the login card footer', () => {
const slogan = wrapper.find(selectors.loginCardFooter)

expect(slogan).toMatchSnapshot()
})
})

describe('when the view is still loading', () => {
test.todo('should display the loading text with the spinner')
test.todo('should not display the error message')
test.todo('should not display the password required form')
const wrapper = getWrapper({ loading: true })
it('should display the loading text with the spinner', () => {
const loading = wrapper.find(selectors.loginCardBody)

expect(wrapper.find(spinnerStub).exists()).toBeTruthy()
expect(loading).toMatchSnapshot()
})
it('should not display the error message', () => {
expect(wrapper.find(selectors.errorMessage).exists()).toBeFalsy()
pascalwengerter marked this conversation as resolved.
Show resolved Hide resolved
})
it('should not display the password required form', () => {
expect(wrapper.find(selectors.publicLinkPasswordRequired).exists()).toBeFalsy()
})
})

describe('when the view is not loading anymore', () => {
test.todo('should not display the loading text and the spinner')
test.todo('should display the error message if "errorMessage" is not empty')
it('should not display the loading text and the spinner', () => {
const wrapper = getWrapper({ loading: false })
const loading = wrapper.find(selectors.loginCardBody)

expect(loading).toMatchSnapshot()
})
it('should display the error message if "errorMessage" is not empty', async () => {
const wrapper = getWrapper({ loading: false })
await wrapper.setData({ errorMessage: 'some-error-message' })

expect(wrapper.find(selectors.errorMessage).exists()).toBeTruthy()
expect(wrapper.find(selectors.errorMessage)).toMatchSnapshot()
})

describe('and when "passwordRequired" is set as true', () => {
let passwordRequiredForm
const wrapper = getWrapper({ loading: false })

beforeEach(async () => {
await wrapper.setData({ passwordRequired: true })
passwordRequiredForm = wrapper.find('form')
})

it('should display the password required form', () => {
expect(passwordRequiredForm.exists()).toBeTruthy()
expect(passwordRequiredForm).toMatchSnapshot()
})

describe('and the password input', () => {
it('should have a computed label', () => {
const passwordInput = passwordRequiredForm.find(textInputStub)

expect(passwordInput).toMatchSnapshot()
})
it('should not display the error message if "inputErrorMessage" is falsy', () => {
const passwordInput = passwordRequiredForm.find(textInputStub)

expect(passwordInput.attributes().errormessage).toBeUndefined()
})
it('should display the error message if "inputErrorMessage" is not empty', async () => {
await wrapper.setData({ inputErrorMessage: 'some input error message' })
const passwordInput = passwordRequiredForm.find(textInputStub)

expect(passwordInput).toMatchSnapshot()
})
})

describe('and the submit button', () => {
it('should be set as disabled if "password" is empty', () => {
expect(wrapper.vm.password).toBeNull()

describe('when "passwordRequired" is set as true', () => {
test.todo('should display the password required form')
const submitButton = wrapper.find(selectors.submitButton)
expect(submitButton.exists()).toBeTruthy()
expect(submitButton).toMatchSnapshot()
})
it('should be set as enabled if "password" is not empty', async () => {
await wrapper.setData({ password: 'some-value' })
expect(wrapper.vm.password).toBe('some-value')

describe('password input', () => {
test.todo('should have a computed label')
test.todo('should display the error message if "inputErrorMessage" is not empty')
test.todo('should not display the error message if "inputErrorMessage" is falsy')
const submitButton = wrapper.find(selectors.submitButton)
expect(submitButton.exists()).toBeTruthy()
expect(submitButton.attributes().disabled).toBeUndefined()
})
})

describe('submit button', () => {
test.todo('should be set as disabled if "password" is empty')
test.todo('should be set as enabled if "password" is not empty')
it('should call "resolvePublicLink" method on form submit', async () => {
const spyResolvePublicLink = jest.spyOn(PublicLink.methods, 'resolvePublicLink')
const wrapper = getMountedWrapper({ loading: false })

await wrapper.setData({ passwordRequired: true, password: 'some-pass' })

const submitButton = wrapper.find(selectors.submitButton)
await submitButton.trigger('submit')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kiranparajuli589 I was able to remove your workaround by switching from .trigger('click') which is correct from a users perspective to a .trigger('submit') which is the technically correct case since we're submitting a form 🤷🏽‍♂️ that was unexpected!


expect(spyResolvePublicLink).toHaveBeenCalledTimes(1)
})
})
})
})

function getMountOptions({
$route = route,
store = getStore({
slogan: theme.general.slogan,
loginLogo: theme.logo,
loginBackgroundImg: theme.loginPage.backgroundImg
}),
loading = false
} = {}) {
return {
localVue,
store,
stubs,
mocks: {
$route
},
data: () => ({
loading: loading
})
}
}

function getWrapper({ loading, store } = {}) {
return shallowMount(component, getMountOptions({ store, loading }))
}

function getMountedWrapper({ loading, store } = {}) {
return mount(component, {
...getMountOptions({ store, loading }),
stubs: {}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PublicLink theming options should display the configuration theme general slogan as the login card footer 1`] = `
<div class="oc-login-card-footer">
<p>
some-slogan
</p>
</div>
`;

exports[`PublicLink theming options should display the logo image inside login card 1`] = `<img src="some-logo" alt="" aria-hidden="true" class="oc-login-logo">`;

exports[`PublicLink theming options should have the background image and page-title set 1`] = `
<div uk-height-viewport="" class="oc-login" style="background-image: url(some-image);">
<h1 class="oc-invisible-sr">some page title</h1>
<div class="oc-login-card uk-position-center"><img src="some-logo" alt="" aria-hidden="true" class="oc-login-logo">
<div class="oc-login-card-body">
<!---->
</div>
<div class="oc-login-card-footer">
<p>
some-slogan
</p>
</div>
</div>
</div>
`;

exports[`PublicLink when the view is not loading anymore and when "passwordRequired" is set as true and the password input should display the error message if "inputErrorMessage" is not empty 1`] = `<oc-text-input-stub id="oc-textinput-3" type="password" clearbuttonaccessiblelabel="" label="Enter password for public link" class="oc-mb-s" errormessage="some input error message"></oc-text-input-stub>`;

exports[`PublicLink when the view is not loading anymore and when "passwordRequired" is set as true and the password input should have a computed label 1`] = `<oc-text-input-stub id="oc-textinput-3" type="password" clearbuttonaccessiblelabel="" label="Enter password for public link" class="oc-mb-s"></oc-text-input-stub>`;

exports[`PublicLink when the view is not loading anymore and when "passwordRequired" is set as true and the submit button should be set as disabled if "password" is empty 1`] = `
<oc-button-stub type="button" disabled="true" size="medium" variation="primary" appearance="filled" justifycontent="center" gapsize="medium" class="oc-login-authorize-button">
<translate-stub tag="span">Continue</translate-stub>
</oc-button-stub>
`;

exports[`PublicLink when the view is not loading anymore and when "passwordRequired" is set as true should display the password required form 1`] = `
<form>
<h2 class="oc-login-card-title">
<translate-stub tag="span">This resource is password-protected.</translate-stub>
</h2>
<oc-text-input-stub id="oc-textinput-3" type="password" clearbuttonaccessiblelabel="" label="Enter password for public link" class="oc-mb-s"></oc-text-input-stub>
<oc-button-stub type="button" disabled="true" size="medium" variation="primary" appearance="filled" justifycontent="center" gapsize="medium" class="oc-login-authorize-button">
<translate-stub tag="span">Continue</translate-stub>
</oc-button-stub>
</form>
`;

exports[`PublicLink when the view is not loading anymore should display the error message if "errorMessage" is not empty 1`] = `
<h2 class="oc-login-card-title oc-login-card-error">
<translate-stub tag="span">An error occurred while loading the public link</translate-stub>
</h2>
`;

exports[`PublicLink when the view is not loading anymore should not display the loading text and the spinner 1`] = `
<div class="oc-login-card-body">
<!---->
</div>
`;

exports[`PublicLink when the view is still loading should display the loading text with the spinner 1`] = `
<div class="oc-login-card-body">
<h2 class="oc-login-card-title">
<translate-stub tag="span">Loading public link…</translate-stub>
</h2>
<oc-spinner-stub arialabel="" size="medium" aria-hidden="true"></oc-spinner-stub>
</div>
`;