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

Add tests for KModal #320

Merged
merged 3 commits into from
Mar 4, 2022
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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

Releases are recorded as git tags in the [Github releases](https://github.com/learningequality/kolibri-design-system/releases) page.

## Version 1.3.1

- [#309] - Add jest testing environment to KDS
- [#311] - Add tests for `KRouterLink`
- [#313] - Add tests for `KButton`
- [#315] - Add tests for `KCheckbox`
- [#320] - Add tests for `KModal`

<!-- Referenced PRs -->
[#309]: https://github.com/learningequality/kolibri-design-system/pull/309
[#311]: https://github.com/learningequality/kolibri-design-system/pull/311
[#313]: https://github.com/learningequality/kolibri-design-system/pull/313
[#315]: https://github.com/learningequality/kolibri-design-system/pull/315
[#320]: https://github.com/learningequality/kolibri-design-system/pull/320

## Version 1.3.0

- [#291] - When tracking input modality with `trackInputModality`, modality is set to keyboard only when the TAB key is pressed
Expand Down
11 changes: 6 additions & 5 deletions lib/KModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
v-if="hasError"
class="visuallyhidden"
>
{{ $tr('errorAlert', { title }) }}
{{ errorMessage }}
</span>
</h1>

Expand Down Expand Up @@ -172,6 +172,11 @@
type: Boolean,
default: false,
},
errorMessage: {
type: String,
default: null,
required: false,
},
},
data() {
return {
Expand Down Expand Up @@ -332,10 +337,6 @@
}
},
},
$trs: {
// error alerts
errorAlert: 'Error in { title }',
},
};

</script>
Expand Down
134 changes: 134 additions & 0 deletions lib/__test__/KModal.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { shallowMount } from '@vue/test-utils';
import KModal from '../KModal';

// jest.mock('kolibri.utils.coreStrings', () => {
// const translations = {
// readReference: 'Reference',
// };
// return {
// $tr: jest.fn(key => {
// return translations[key];
// }),
// };
// });

describe('KModal component', () => {
it(`smoke test`, () => {
const wrapper = shallowMount(KModal);
expect(wrapper.exists()).toBe(true);
});

describe('props', () => {
it(`a title should appear`, () => {
const title = 'test title';
const wrapper = shallowMount(KModal, {
propsData: {
title: title,
},
});
expect(wrapper.find('.title').text()).toEqual(title);
});
it(`text should appear on the submit button`, () => {
const submitText = 'hello';
const onClick = jest.fn();
const wrapper = shallowMount(KModal, {
propsData: {
submitText: submitText,
},
listeners: { submit: onClick },
});
expect(wrapper.findComponent({ name: 'KButton' }).props().text).toEqual(submitText);
});

it(`text should appear on the close button`, () => {
let cancelText = 'goodbye';
const onClick = jest.fn();
const wrapper = shallowMount(KModal, {
propsData: {
cancelText: cancelText,
},
listeners: { cancel: onClick },
});
expect(wrapper.findComponent({ name: 'KButton' }).props().text).toEqual(cancelText);
Copy link
Member

Choose a reason for hiding this comment

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

Never again will I import the component now that I know I can pass an object with the name property!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

YES! I learned that, I think, from reading someone else's test, and 🤯 .

});

it(`submit button is disabled if submitDisabled is true`, () => {
let submitText = 'hello';
const onClick = jest.fn();
const wrapper = shallowMount(KModal, {
propsData: {
submitText: submitText,
submitDisabled: true,
},
listeners: { submit: onClick },
});
expect(wrapper.findComponent({ name: 'KButton' }).props().disabled).toBeTruthy();
});

it(`cancel button is disabled if cancelDisabled is true`, () => {
let cancelText = 'goodbye';
const onClick = jest.fn();
const wrapper = shallowMount(KModal, {
propsData: {
cancelText: cancelText,
cancelDisabled: true,
},
listeners: { cancel: onClick },
});
expect(wrapper.findComponent({ name: 'KButton' }).props().disabled).toBeTruthy();
});

it(`size of modal can be determined with string prop`, () => {
const wrapper = shallowMount(KModal, {
propsData: {
size: 'large',
},
});
expect(wrapper.find('.modal').element.style['width']).toEqual('100%');
});

it(`size of modal can be determined by integer prop`, () => {
const wrapper = shallowMount(KModal, {
propsData: {
size: 789,
},
});
expect(wrapper.find('.modal').element.style['width']).toEqual('789px');
});

it(`error message is in a visually hidden span if hasError is true`, () => {
const error = 'watch out for the error';
const wrapper = shallowMount(KModal, {
propsData: {
hasError: true,
errorMessage: error,
},
});
expect(wrapper.find('.visuallyhidden').element).toBeTruthy();
expect(wrapper.find('.visuallyhidden').text()).toContain(error);
});
});
describe('slots', () => {
it(`content is displayed in default <slot>`, () => {
const slot = '<div><p>main content appears here</p></div>';
const wrapper = shallowMount(KModal, {
slots: {
default: slot,
},
});
expect(wrapper.find('.content').text()).toContain('main content appears here');
});
it(`alternative content is displayed below main content if actions <slot> is used`, () => {
const slot = '<div><p>main content appears here</p></div>';
const actionsSlot = '<div><p class="actionsSlot">actions content</p></div>';
const wrapper = shallowMount(KModal, {
slots: {
default: slot,
actions: actionsSlot,
},
});
expect(wrapper.find('.content').text()).toContain('main content appears here');
expect(wrapper.find('.actions').text()).toContain('actions content');
});
});
});