Skip to content

Commit

Permalink
feat: component testing setup with vitest and jest-dom (#46)
Browse files Browse the repository at this point in the history
* fix: hidden choices keyboard selection issue (#45)

* fix: hidden choice keyboard selection

* chore: 2.11.1 changelog

* fix: issue where pressing space wouldn't continue

* update to packages + added pinia testing package

* tests starting to work

* pnpm lock

* tests working in workspace but intellisense on jest-dom lost...

* fixed build command issue
  • Loading branch information
liana-p committed Apr 10, 2023
1 parent f67826c commit cef092e
Show file tree
Hide file tree
Showing 16 changed files with 1,673 additions and 493 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Narrat changelog

## [2.11.1] Small bugfixes

* fix: there was an issue where it was possible to select a "hidden" choice (choice that didn't pass a condition) by pressing the corresponding keyboard number

## [2.11.0] Audio volume improvement and saves fix

* Audio volume in an individual audio file's config wasn't used properly and is now correctly mixed with the relevant volume for channels (by [@jornvandebeek](https://github.com/jornvandebeek))
Expand Down
1 change: 1 addition & 0 deletions packages/narrat/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"vitest.enable": true,
"editor.tabSize": 2,
"files.exclude": {
"**/*.rpyc": true,
Expand Down
136 changes: 0 additions & 136 deletions packages/narrat/README.md

This file was deleted.

17 changes: 15 additions & 2 deletions packages/narrat/examples/games/default/scripts/default.nar
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
main:
// Note: This script is a dev testing thing and is a mess of random test commands, not meant to make sense
set config.characters.characters.player.name "Someone"
talk player idle "I like @@bread"
// jump test_choice_conditions
// set config.characters.characters.player.name "Someone"
// talk player idle "I like @@bread"
jump quest_demo

test_choice_conditions:
set data.falseCondition false
choice:
"Test"
"Option 1":
"Hello 1"
"Option 2":
"Hello 2"
"Option 3" if $data.falseCondition:
"Hello 3"
"End of test"

test_skill_checks:
choice:
"test skill roll"
Expand Down
24 changes: 16 additions & 8 deletions packages/narrat/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "narrat",
"version": "2.11.0",
"version": "2.11.1",
"description": "narrat narrative engine",
"main": "dist/narrat.umd.js",
"module": "dist/narrat.es.js",
Expand All @@ -19,7 +19,8 @@
],
"scripts": {
"test-unit": "vitest",
"coverage": "vitest run --coverage",
"coverage": "vitest --coverage",
"test": "pnpm run test-unit",
"start": "pnpm run dev",
"rpg": "pnpm run run-example rpg",
"build-rpg": "pnpm run build-example rpg",
Expand All @@ -34,7 +35,7 @@
"dev": "pnpm run run-example default",
"generate-types": "vue-tsc --emitDeclarationOnly --outDir dist && pnpm run fix-type-aliases",
"fix-type-aliases": "tsc-alias -p tsconfig.json",
"build": "rimraf lib && vue-tsc --noEmit && vite build && pnpm run generate-types",
"build": "rimraf lib && vite build && pnpm run generate-types",
"preview": "vite preview",
"lint": "eslint src --ext=.js,.jsx,.ts,.tsx",
"preversion": "pnpm run build && pnpm run lint",
Expand Down Expand Up @@ -64,11 +65,18 @@
"vue3-mq": "^3.0.0"
},
"devDependencies": {
"@pinia/testing": "^0.0.16",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/vue": "^7.0.0",
"@types/howler": "^2.2.3",
"@types/jest": "^29.5.0",
"@types/js-yaml": "^4.0.5",
"@types/jsdom": "^21.1.1",
"@types/testing-library__jest-dom": "^5.14.5",
"@typescript-eslint/eslint-plugin": "^5.30.0",
"@typescript-eslint/parser": "^5.30.0",
"@vitejs/plugin-vue": "^3.0.0",
"chai-style": "^1.0.3",
"cross-env": "^7.0.3",
"es6-promise": "^4.2.8",
"eslint": "^8.18.0",
Expand All @@ -82,18 +90,18 @@
"eslint-plugin-vue": "^9.1.1",
"jsdom": "^20.0.0",
"kolorist": "^1.5.1",
"pinia": "^2.0.14",
"pinia": "^2.0.34",
"prettier": "^2.7.1",
"shelljs": "^0.8.5",
"shx": "^0.3.4",
"ts-prune": "^0.10.3",
"tsc-alias": "^1.6.11",
"tslib": "^2.4.0",
"typescript": "^4.7.4",
"vite": "^3.0.0",
"typescript": "^4.9.5",
"vite": "^3.2.5",
"vite-plugin-windicss": "^1.8.7",
"vitest": "^0.19.1",
"vue": "^3.2.37",
"vitest": "^0.30.0",
"vue": "^3.2.47",
"vue-tsc": "^0.38.2",
"windicss": "^3.5.6"
}
Expand Down
15 changes: 11 additions & 4 deletions packages/narrat/src/components/tooltips/tooltips-ui.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
<template>
<div class="tooltips-ui">
<FloatingTooltip v-if="tooltip" v-bind="tooltip" />
<FloatingTooltip v-if="floatingTooltip" v-bind="floatingTooltip" />
</div>
</template>
<script lang="ts" setup>
import { tooltipsConfig } from '@/config';
import { useTooltips } from '@/stores/tooltip-store';
import { computed } from 'vue';
import FloatingTooltip from '../utils/floating-tooltip.vue';
import FloatingTooltip, {
FloatingTooltipProps,
} from '../utils/floating-tooltip.vue';
const tooltip = computed(() => useTooltips().tooltip);
const width = computed(() => tooltipsConfig().options.width);
const floatingTooltip = computed(() => {
const tooltip = useTooltips().tooltip;
return {
...tooltip,
screenMargin: tooltipsConfig().options.screenEdgesMinimumMargin ?? 5,
} as FloatingTooltipProps;
});
</script>
<style></style>
18 changes: 18 additions & 0 deletions packages/narrat/src/components/utils/__tests__/alert-modal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, it, expect } from 'vitest';
import { render, screen } from '@testing-library/vue';
import AlertModal from '../alert-modal.vue';

describe('AlertModal.vue test', () => {
it('renders a modal with the right title and text', () => {
const title = 'Alert Modal test title';
const text = 'Alert modal test text';
render(AlertModal, {
props: {
title,
text,
}
});
screen.getByText(title);
screen.getByText(text);
})
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, it, expect } from 'vitest';
import { render, screen } from '@testing-library/vue';
import FloatingTooltip from '../floating-tooltip.vue';

const title = 'Tooltip test title';
const text = 'Tooltip test text';
const width = 200;
const maxWidth = window.innerWidth;
const maxHeight = window.innerHeight;
const screenMargin = 5;
describe('FloatingTooltip.vue tests', () => {
it('renders a tooltip with the right title and text', () => {
render(FloatingTooltip, {
props: {
title,
text,
width: 200,
x: 0,
y: 0,
},
});
screen.getByText(title);
screen.getByText(text);
});

it('renders tooltips at the right width', () => {
const width = 200;
render(FloatingTooltip, {
props: {
title,
text,
width,
x: 0,
y: 0,
},
});
const titleElement = screen.getByText(title);
const tooltip: HTMLElement = titleElement.parentElement!;
expect(tooltip).toBeTruthy();
expect(tooltip).toHaveStyle({ width: `${width}px` });
});

it('adapts the position of the tooltip to the screen size', () => {
render(FloatingTooltip, {
props: {
title,
text,
width,
x: maxWidth,
y: 0,
screenMargin,
},
});
const titleElement = screen.getByText(title);
const tooltip: HTMLElement = titleElement.parentElement!;
const correctX = maxWidth - width - screenMargin;
expect(tooltip).toBeTruthy();
expect(tooltip).toHaveStyle({ left: `${correctX}px` });
});
});
11 changes: 7 additions & 4 deletions packages/narrat/src/components/utils/floating-tooltip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</template>

<script lang="ts" setup>
import { useConfig } from '@/lib';
import { computed, onMounted, ref } from 'vue';
export interface FloatingTooltipProps {
Expand All @@ -21,20 +22,22 @@ export interface FloatingTooltipProps {
width: number;
x: number;
y: number;
screenMargin?: number;
}
const height = ref(150);
const element = ref<HTMLElement | null>(null);
const props = defineProps<FloatingTooltipProps>();
const style = computed((): any => {
const screenMargin = props.screenMargin ?? 5;
const cssStyle: any = {};
let x = props.x - props.width / 2;
x = Math.min(x, window.innerWidth - props.width - 5);
x = Math.max(5, x);
x = Math.min(x, window.innerWidth - props.width - screenMargin);
x = Math.max(screenMargin, x);
cssStyle.left = `${x}px`;
let y = window.innerHeight - props.y;
y = Math.max(y, 5);
y = Math.min(y, window.innerHeight - height.value - 5);
y = Math.max(y, screenMargin);
y = Math.min(y, window.innerHeight - height.value - screenMargin);
cssStyle.bottom = `${y}px`;
cssStyle.width = `${props.width}px`;
return cssStyle;
Expand Down
1 change: 1 addition & 0 deletions packages/narrat/src/config/tooltips-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const TooltipsConfigSchema = Type.Object({
delay: Type.Optional(Type.Number()),
width: Type.Number(),
keywordsPrefix: Type.String(),
screenEdgesMinimumMargin: Type.Optional(Type.Number()),
}),
tooltips: Type.Array(
Type.Object({
Expand Down
Loading

0 comments on commit cef092e

Please sign in to comment.