devin schumacher
devin schumacher
NNuxt
Created by devin schumacher on 12/12/2024 in #❓・help
unit test help
<template>
<client-only>
<u-button
:icon="isDark ? 'i-lucide-moon' : 'i-lucide-sun'"
color="neutral"
variant="ghost"
aria-label="Toggle color mode"
@click="isDark = !isDark"
></u-button>
<template #fallback>
<div class="size-8"></div>
</template>
</client-only>
</template>

<script setup lang="ts">
import { useColorMode } from '#imports'

const colorMode = useColorMode()

const isDark = computed({
get() {
return colorMode.value === 'dark'
},
set() {
colorMode.preference = colorMode.value === 'dark' ? 'light' : 'dark'
}
})
</script>
<template>
<client-only>
<u-button
:icon="isDark ? 'i-lucide-moon' : 'i-lucide-sun'"
color="neutral"
variant="ghost"
aria-label="Toggle color mode"
@click="isDark = !isDark"
></u-button>
<template #fallback>
<div class="size-8"></div>
</template>
</client-only>
</template>

<script setup lang="ts">
import { useColorMode } from '#imports'

const colorMode = useColorMode()

const isDark = computed({
get() {
return colorMode.value === 'dark'
},
set() {
colorMode.preference = colorMode.value === 'dark' ? 'light' : 'dark'
}
})
</script>
7 replies
NNuxt
Created by devin schumacher on 12/12/2024 in #❓・help
unit test help
and component code below:
7 replies
NNuxt
Created by devin schumacher on 12/12/2024 in #❓・help
unit test help
test code ^^
7 replies
NNuxt
Created by devin schumacher on 12/12/2024 in #❓・help
unit test help
import { mockNuxtImport, renderSuspended } from '@nuxt/test-utils/runtime';
import { screen } from '@testing-library/vue';
import userEvent from '@testing-library/user-event';
import { describe, it, expect, beforeEach } from 'vitest';

import ColorModeButton from '~/components/ColorModeButton.vue';

const colorMode = {
_preference: 'light' as 'light' | 'dark',
get preference() {
return this._preference;
},
set preference(val: 'light' | 'dark') {
this._preference = val;
},
get value() {
return this._preference;
}
};

mockNuxtImport('useColorMode', () => {
return () => colorMode;
});

describe('ColorModeButton', () => {
beforeEach(() => {
colorMode.preference = 'light';
});

it('should render a toggle button with sun icon in light mode', async () => {
await renderSuspended(ColorModeButton);
const button = await screen.findByRole('button', { name: /toggle color mode/i });
const icon = await button.querySelector('span')
expect(icon).toHaveClass('i-lucide:sun');
});

// TODO: fix this test...cant figure out why clicking on the button wont switch the icon
// it('should display a moon icon when the sun is clicked', async () => {
// const user = userEvent.setup()
// await renderSuspended(ColorModeButton);
// const button = await screen.findByRole('button', { name: /toggle color mode/i });
// await user.click(button);
// const icon = await button.querySelector('span');

// expect(icon).toHaveClass('i-lucide:moon');
// });
});
import { mockNuxtImport, renderSuspended } from '@nuxt/test-utils/runtime';
import { screen } from '@testing-library/vue';
import userEvent from '@testing-library/user-event';
import { describe, it, expect, beforeEach } from 'vitest';

import ColorModeButton from '~/components/ColorModeButton.vue';

const colorMode = {
_preference: 'light' as 'light' | 'dark',
get preference() {
return this._preference;
},
set preference(val: 'light' | 'dark') {
this._preference = val;
},
get value() {
return this._preference;
}
};

mockNuxtImport('useColorMode', () => {
return () => colorMode;
});

describe('ColorModeButton', () => {
beforeEach(() => {
colorMode.preference = 'light';
});

it('should render a toggle button with sun icon in light mode', async () => {
await renderSuspended(ColorModeButton);
const button = await screen.findByRole('button', { name: /toggle color mode/i });
const icon = await button.querySelector('span')
expect(icon).toHaveClass('i-lucide:sun');
});

// TODO: fix this test...cant figure out why clicking on the button wont switch the icon
// it('should display a moon icon when the sun is clicked', async () => {
// const user = userEvent.setup()
// await renderSuspended(ColorModeButton);
// const button = await screen.findByRole('button', { name: /toggle color mode/i });
// await user.click(button);
// const icon = await button.querySelector('span');

// expect(icon).toHaveClass('i-lucide:moon');
// });
});
7 replies
NNuxt
Created by devin schumacher on 11/8/2024 in #❓・help
@nuxt/test-utils.... config hellP
ty
12 replies
NNuxt
Created by devin schumacher on 11/8/2024 in #❓・help
@nuxt/test-utils.... config hellP
No description
12 replies
NNuxt
Created by devin schumacher on 11/8/2024 in #❓・help
@nuxt/test-utils.... config hellP
No description
12 replies
NNuxt
Created by devin schumacher on 11/8/2024 in #❓・help
@nuxt/test-utils.... config hellP
my understanding is jsdom and happy-dom are Dom Renderers, not runtimes, but the nuxt docs says "you can choose between happy-dom and jsdom for a runtime Nuxt environment" which is either wrong, or confusing, or i dont know what im talking about
12 replies
NNuxt
Created by devin schumacher on 11/8/2024 in #❓・help
@nuxt/test-utils.... config hellP
wtf
12 replies