Skip to content

Commit

Permalink
refactor: dropdown use script setup
Browse files Browse the repository at this point in the history
  • Loading branch information
wxsms committed Nov 23, 2021
1 parent d9193dd commit e715aaf
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 148 deletions.
23 changes: 16 additions & 7 deletions docs/.vitepress/components/dropdown/advanced.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,26 @@
</section>
</template>
<script>
import { onMounted, ref } from 'vue';
export default {
data() {
setup() {
const show = ref(false);
const dropdown = ref(null);
const ele = ref([]);
const selected = ref([]);
onMounted(() => {
ele.value.push(dropdown.value.$el);
});
return {
show: false,
ele: [],
selected: [],
ele,
dropdown,
show,
selected,
};
},
mounted() {
this.ele.push(this.$refs.dropdown.$el);
},
};
</script>
<style>
Expand Down
9 changes: 9 additions & 0 deletions src/__test__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,12 @@ export const triggerKey = (el, key, type = 'down') => {
el.dispatchEvent(eventObj);
}
};

export function windowClick(target) {
let e = new Event('click');
Object.defineProperty(e, 'target', {
writable: false,
value: target,
});
window.dispatchEvent(e);
}
19 changes: 10 additions & 9 deletions src/components/dropdown/Dropdown.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { createWrapper, nextTick, triggerEvent } from '../../__test__/utils';
import {
createWrapper,
nextTick,
triggerEvent,
windowClick,
} from '../../__test__/utils';
import Dropdown from './Dropdown';

function appendToBodyVm() {
Expand Down Expand Up @@ -176,8 +181,7 @@ describe('Dropdown', () => {
expect(dropdown.classes()).not.toContain('open');
await triggerEvent(trigger, 'click');
expect(dropdown.classes()).toContain('open');
// Simulate a window click
wrapper.vm.$refs.dropdown.windowClicked({ target: document.body });
windowClick(document.body);
await nextTick();
expect(dropdown.classes()).not.toContain('open');
});
Expand Down Expand Up @@ -207,18 +211,15 @@ describe('Dropdown', () => {
const dropdown = wrapper;
expect(dropdown.element.tagName.toLowerCase()).toEqual('div');
expect(dropdown.classes()).toContain('open');
// Simulate a window click
wrapper.vm.$refs.test.windowClicked({ target: wrapper.vm.$refs.li1 });
windowClick(wrapper.vm.$refs.li1);
await nextTick();
expect(dropdown.classes()).toContain('open');
// Simulate a window click
wrapper.vm.$refs.test.windowClicked({ target: document.body });
windowClick(document.body);
await nextTick();
expect(dropdown.classes()).not.toContain('open');
wrapper.vm.show = true;
await nextTick();
// Simulate a window click
wrapper.vm.$refs.test.windowClicked({ target: wrapper.vm.$refs.li2 });
windowClick(wrapper.vm.$refs.li2);
await nextTick();
expect(dropdown.classes()).not.toContain('open');
});
Expand Down

0 comments on commit e715aaf

Please sign in to comment.