Skip to content

Commit

Permalink
[ONL-7918] ec-table-sort to TS (#1690)
Browse files Browse the repository at this point in the history
* [ONL-7918] fix unit test when using imported types for props

* [ONL-7918] migrate sort direction enum

* [ONL-7918] migrate ec-table-sort

* [ONL-7918] fix broken tests due to fix

* [ONL-7918] add missing default value

* [ONL-7918] revert comment in props interface eslint rule

* [ONL-7918] remove jest fix and use prop interface in component

* [ONL-7918] ts fix in unit test

* [ONL-7918] rename sort-direction.ts to index.ts

* 2.2.45
  • Loading branch information
raulwwq0 authored Sep 25, 2023
1 parent 0a7b47b commit adf2647
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 42 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ebury/chameleon-components",
"version": "2.2.44",
"version": "2.2.45",
"main": "src/main.js",
"sideEffects": false,
"author": "Ebury Team (http://labs.ebury.rocks/)",
Expand Down
2 changes: 1 addition & 1 deletion src/components/ec-smart-table/ec-smart-table.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mount } from '@vue/test-utils';
import { h, markRaw } from 'vue';

import * as SortDirection from '../../enums/sort-direction';
import { SortDirection } from '../../enums';
import EcSmartTable from './ec-smart-table.vue';

describe('EcSmartTable', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ec-smart-table/ec-smart-table.story.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
computed, markRaw, onBeforeUnmount, ref,
} from 'vue';

import * as SortDirection from '../../enums/sort-direction';
import { SortDirection } from '../../enums';
import * as SortDirectionCycle from '../../enums/sort-direction-cycle';
import EcDateRangeFilter from '../ec-date-range-filter';
import EcIcon from '../ec-icon';
Expand Down
3 changes: 2 additions & 1 deletion src/components/ec-smart-table/ec-smart-table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ import {
import useEcPagination from '../../composables/use-ec-pagination';
import useEcSorting from '../../composables/use-ec-sorting';
import * as SortDirection from '../../enums/sort-direction';
import { SortDirection } from '../../enums';
import * as SortDirectionCycle from '../../enums/sort-direction-cycle';
import EcLoading from '../ec-loading';
import EcSmartTableHeading from '../ec-smart-table-heading';
Expand Down Expand Up @@ -225,3 +225,4 @@ function getEcTableSlots() {
return tableSlots;
}
</script>
../../enums
2 changes: 1 addition & 1 deletion src/components/ec-table-head/ec-table-head.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mount } from '@vue/test-utils';

import { withMockedConsole } from '../../../tests/utils/console';
import * as SortDirection from '../../enums/sort-direction';
import { SortDirection } from '../../enums';
import EcTableHead from './ec-table-head.vue';

describe('EcTableHead', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { mount } from '@vue/test-utils';
import { type ComponentMountingOptions, mount } from '@vue/test-utils';

import { withMockedConsole } from '../../../tests/utils/console';
import * as SortDirection from '../../enums/sort-direction';
import type { CVueWrapper } from '../../../tests/utils/global';
import { SortDirection } from '../../enums';
import EcTableSort from './ec-table-sort.vue';
import type { TableSortProps } from './types';

describe('EcTableSort', () => {
function mountEcTableSort(props, mountOpts) {
function mountEcTableSort(props?: TableSortProps, mountOpts?: ComponentMountingOptions<TableSortProps>) {
return mount(EcTableSort, {
props,
...mountOpts,
});
}) as unknown as CVueWrapper;
}

it('should render as expected', () => {
Expand Down Expand Up @@ -37,14 +38,6 @@ describe('EcTableSort', () => {
const wrapper = mountEcTableSort({ direction: SortDirection.DESC });
expect(wrapper.element).toMatchSnapshot();
});

it('should validate direction prop', () => {
withMockedConsole((errorSpy, warnSpy) => {
mountEcTableSort({ direction: 'invalid' });
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy.mock.calls[0][0]).toContain('Invalid prop: custom validator check failed for prop "direction"');
});
});
});

describe('@events', () => {
Expand Down
31 changes: 17 additions & 14 deletions src/components/ec-table-sort/ec-table-sort.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,34 @@
</a>
</template>

<script setup>
<script setup lang="ts">
import { computed } from 'vue';
import * as SortDirection from '../../enums/sort-direction';
import { SortDirection } from '../../enums';
import EcIcon from '../ec-icon';
import { IconName } from '../ec-icon/types';
import type { SortDirectionType, TableSortEvent, TableSortEvents } from './types';
const emit = defineEmits(['sort']);
const emit = defineEmits<{
'sort': [value: TableSortEvents[TableSortEvent.SORT]],
}>();
const props = defineProps({
direction: {
type: String,
default: null,
validator(value) {
return value === null || value === '' || [SortDirection.ASC, SortDirection.DESC].includes(value);
},
},
interface TableSortProps {
direction?: SortDirectionType,
}
const props = withDefaults(defineProps<TableSortProps>(), {
direction: null,
});
const isAsc = computed(() => props.direction === SortDirection.ASC);
const isDesc = computed(() => props.direction === SortDirection.DESC);
const icon = computed(() => {
if (isAsc.value || isDesc.value) {
return 'simple-arrow-drop-down';
return IconName.SimpleArrowDropDown;
}
return 'simple-arrow-up-down';
return IconName.SimpleArrowUpDown;
});
const directionTitle = computed(() => {
Expand All @@ -61,7 +63,7 @@ const directionTitle = computed(() => {
return 'Not sorted';
});
function onSort() {
function onSort(): void {
emit('sort', props.direction);
}
</script>
Expand Down Expand Up @@ -94,3 +96,4 @@ function onSort() {
}
}
</style>
../../enums
File renamed without changes.
15 changes: 15 additions & 0 deletions src/components/ec-table-sort/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { SortDirection } from '../../enums';

export type SortDirectionType = SortDirection | '' | null;

export enum TableSortEvent {
SORT = 'sort',
}

export interface TableSortEvents {
[TableSortEvent.SORT]: SortDirectionType,
}

export interface TableSortProps {
direction?: SortDirectionType,
}
2 changes: 1 addition & 1 deletion src/components/ec-table/ec-table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mount } from '@vue/test-utils';
import { defineComponent, h } from 'vue';

import { withMockedConsole } from '../../../tests/utils/console';
import * as SortDirection from '../../enums/sort-direction';
import { SortDirection } from '../../enums';
import EcTable from './ec-table.vue';

function mountEcTable(props, mountOpts) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ec-table/ec-table.story.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { action } from '@storybook/addon-actions';

import * as SortDirection from '../../enums/sort-direction';
import { SortDirection } from '../../enums';
import EcIcon from '../ec-icon/ec-icon.vue';
import EcOptionCard from '../ec-option-card';
import EcTable from './ec-table.vue';
Expand Down
2 changes: 1 addition & 1 deletion src/composables/use-ec-sorting/use-ec-sorting.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ref, unref } from 'vue';

import * as SortDirection from '../../enums/sort-direction';
import { SortDirection } from '../../enums';
import * as SortDirectionCycle from '../../enums/sort-direction-cycle';

export default function useEcSorting({
Expand Down
2 changes: 1 addition & 1 deletion src/composables/use-ec-sorting/use-ec-sorting.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as SortDirection from '../../enums/sort-direction';
import { SortDirection } from '../../enums';
import * as SortDirectionCycle from '../../enums/sort-direction-cycle';
import useEcSorting from './use-ec-sorting';

Expand Down
4 changes: 4 additions & 0 deletions src/enums/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum SortDirection {
ASC = 'asc',
DESC = 'desc',
}
2 changes: 1 addition & 1 deletion src/enums/sort-direction-cycle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as SortDirection from './sort-direction';
import { SortDirection } from '.';

export const LOWEST_FIRST = [SortDirection.ASC, SortDirection.DESC];
export const HIGHEST_FIRST = [SortDirection.DESC, SortDirection.ASC];
2 changes: 0 additions & 2 deletions src/enums/sort-direction.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as SortDirection from './enums/sort-direction';
import { SortDirection } from './enums';
import * as SortDirectionCycle from './enums/sort-direction-cycle';
import * as TooltipPlacement from './enums/tooltip-placement';

Expand Down

1 comment on commit adf2647

@vercel
Copy link

@vercel vercel bot commented on adf2647 Sep 25, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

chameleon – ./

chameleon-dead-plane.vercel.app
chameleon-git-master-ebury.vercel.app
chameleon-ebury.vercel.app

Please sign in to comment.