Commit 7cfef3af authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Add additional jest tests

Add tests for the `omitEmptyProperties` method

Minor refactor of the `omitEmptyProperties` method
and adds some jest tests.
parent d88eacb0
import Vue from 'vue'; import Vue from 'vue';
import { parseBoolean } from './lib/utils/common_utils'; import { parseBoolean, omitEmptyProperties } from './lib/utils/common_utils';
import ConfirmDanger from './vue_shared/components/confirm_danger/confirm_danger.vue'; import ConfirmDanger from './vue_shared/components/confirm_danger/confirm_danger.vue';
const omitEmptyProperties = (fields) => {
return Object.entries(fields).reduce((acc, [key, value]) => {
if (value) {
return { ...acc, [key]: value };
}
return acc;
}, {});
};
export default () => { export default () => {
const el = document.querySelector('.js-confirm-danger'); const el = document.querySelector('.js-confirm-danger');
if (!el) return null; if (!el) return null;
......
...@@ -746,3 +746,25 @@ export const isLoggedIn = () => Boolean(window.gon?.current_user_id); ...@@ -746,3 +746,25 @@ export const isLoggedIn = () => Boolean(window.gon?.current_user_id);
*/ */
export const convertArrayOfObjectsToCamelCase = (array) => export const convertArrayOfObjectsToCamelCase = (array) =>
array.map((o) => convertObjectPropsToCamelCase(o)); array.map((o) => convertObjectPropsToCamelCase(o));
/**
* This method takes an object and omits any properties
* that are null, undefined, false or empty. The returned
* object only includes properties that have a `truthy` value.
*
* @param {Object} obj - Target object to be checked
* @returns {Object} Resulting object with only truthy properties
*/
export const omitEmptyProperties = (obj = {}) => {
const entries = Object.entries(obj);
if (!entries.length) {
return obj;
}
return entries.reduce((acc, [key, value]) => {
if (!value) {
return acc;
}
return { ...acc, [key]: value };
}, {});
};
...@@ -1051,4 +1051,30 @@ describe('common_utils', () => { ...@@ -1051,4 +1051,30 @@ describe('common_utils', () => {
expect(result).toEqual([{ hello: '' }, { helloWorld: '' }]); expect(result).toEqual([{ hello: '' }, { helloWorld: '' }]);
}); });
}); });
describe('omitEmptyProperties', () => {
let res;
const empty = { foo: null, bar: undefined, baz: '', qux: false };
const values = { a: 'a', b: 1, c: { d: 'd' }, e: ['f'] };
beforeEach(() => {
res = Object.keys(
commonUtils.omitEmptyProperties({
...empty,
...values,
}),
);
});
it('returns an object with properties that have a value', () => {
const valueKeys = Object.keys(values);
expect(res).toEqual(valueKeys);
});
it('ignores properties that are empty, nullable or falsy', () => {
Object.keys(empty).forEach((key) => {
expect(res).not.toContain(key);
});
});
});
}); });
...@@ -10,6 +10,7 @@ describe('Confirm Danger Modal', () => { ...@@ -10,6 +10,7 @@ describe('Confirm Danger Modal', () => {
const phrase = 'En Taro Adun'; const phrase = 'En Taro Adun';
const buttonText = 'Click me!'; const buttonText = 'Click me!';
const buttonClass = 'gl-w-full'; const buttonClass = 'gl-w-full';
const buttonVariant = 'info';
const modalId = CONFIRM_DANGER_MODAL_ID; const modalId = CONFIRM_DANGER_MODAL_ID;
const findBtn = () => wrapper.findComponent(GlButton); const findBtn = () => wrapper.findComponent(GlButton);
...@@ -21,6 +22,7 @@ describe('Confirm Danger Modal', () => { ...@@ -21,6 +22,7 @@ describe('Confirm Danger Modal', () => {
propsData: { propsData: {
buttonText, buttonText,
buttonClass, buttonClass,
buttonVariant,
phrase, phrase,
...props, ...props,
}, },
...@@ -57,6 +59,10 @@ describe('Confirm Danger Modal', () => { ...@@ -57,6 +59,10 @@ describe('Confirm Danger Modal', () => {
expect(findBtn().classes()).toContain(buttonClass); expect(findBtn().classes()).toContain(buttonClass);
}); });
it('passes `buttonVariant` prop to button', () => {
expect(findBtn().attributes('variant')).toBe(buttonVariant);
});
it('will emit `confirm` when the modal confirms', () => { it('will emit `confirm` when the modal confirms', () => {
expect(wrapper.emitted('confirm')).toBeUndefined(); expect(wrapper.emitted('confirm')).toBeUndefined();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment