Skip to content

Commit

Permalink
introducing component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexRoehm committed Sep 5, 2022
1 parent 598ea66 commit 9e562cf
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 29 deletions.
1 change: 1 addition & 0 deletions base/PodcastDetailValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default function validation(
});
nonEmpty(fields, 'title', errors);
nonEmpty(fields, 'author', errors);
nonEmpty(fields, 'slug', errors);
selectionEmpty(fields, 'language', errors);
selectionEmpty(fields, 'category', errors);
selectionEmpty(fields, 'type', errors);
Expand Down
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"title": "Please enter a title",
"author": "Please enter an author",
"language": "Please select the language",
"slug": "Please enter a unique slug for the podcast",
"category": "Please select a category for your content",
"type": "Please select the publishing type",
"link": "Please enter the link",
Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview",
"test": "vitest",
"test:unit": "vitest run --dir ./tests/unit",
"test:comp": "vitest run --config ./tests/components/vitest.config.js --dir ./tests/components",
"test:e2e": "vitest run --dir ./tests/e2e",
"test:int": "vitest run --dir ./tests/integration",
"test": "yarn test:unit && yarn test:comp && yarn test:e2e",
"start": "ts-node src/index.ts",
"typeorm": "typeorm-ts-node-commonjs",
"migration": "typeorm-ts-node-commonjs -d ./server/db/datasource.ts migration:generate ./server/db/migrations/auth"
Expand All @@ -17,7 +21,10 @@
"@nuxtjs/color-mode": "^3.1.4",
"@nuxtjs/tailwindcss": "^5.3.0",
"@types/node": "^16.11.10",
"@vitejs/plugin-vue": "^3.0.3",
"@vue/test-utils": "^2.0.2",
"autoprefixer": "^10.4.8",
"jsdom": "^20.0.0",
"nuxt": "3.0.0-rc.8",
"postcss": "^8.4.14",
"sqlite3": "^5.0.10",
Expand Down
2 changes: 1 addition & 1 deletion server/db/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export const AppDataSource = new DataSource({
entities: [Podcast, Serie, Episode, Enumerator, User, Session],
logging: true,
synchronize: true,
});
});
22 changes: 7 additions & 15 deletions server/db/dbsigleton.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import "reflect-metadata";
import { DataSource, Entity } from "typeorm";
import Podcast from "./entities/Podcast";
import { DataSource } from "typeorm";
import Enumerator from "./entities/Enumerator";
import { fillDefaultEnums, addAdmin } from "./initdata";
import Episode from "./entities/Episode";
import Serie from "./entities/Serie";
import User from "./entities/User";
import Session from "./entities/Session";
import { AppDataSource } from "./datasource";

var dataSource = AppDataSource
var dataSource: DataSource = null

export function setAnotherFilename(filename) {
dataSource = new DataSource({
type: "sqlite",
database: filename,
entities: [Podcast, Serie, Episode, Enumerator, User, Session],
logging: true,
synchronize: true,
});
export function setTestDataSource(TstDataSource: DataSource) {
dataSource = TstDataSource
}

export default async function getDataSource(): Promise<DataSource> {
if (!dataSource)
dataSource = AppDataSource;
if (dataSource.isInitialized) return dataSource;
else {
console.log("init db")
Expand All @@ -40,3 +31,4 @@ export default async function getDataSource(): Promise<DataSource> {
return dataSource;
}
}

25 changes: 13 additions & 12 deletions server/db/entities/Enumerator.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from "typeorm";
import IEnumerator, { emptyEnum } from "../../../base/types/IEnumerator";

export function getEnumerator(json: Object): Enumerator {
const enumerator = new Enumerator();
enumerator.displaytext = json["displaytext"] as string;
enumerator.shorttext = json["shorttext"] as string;
enumerator.parentCategory = json["parentCategory"] as string;
enumerator.enumkey_id = json["enumkey_id"] as number;
enumerator.enumvalue_id = json["enumvalue_id"] as number;
if (json.hasOwnProperty("id")) enumerator.id = json["id"] as number;
return enumerator;
}
import IEnumerator from "../../../base/types/IEnumerator";

@Entity()
export default class Enumerator extends BaseEntity implements IEnumerator{
Expand All @@ -33,3 +22,15 @@ export default class Enumerator extends BaseEntity implements IEnumerator{
@Column("int")
enumvalue_id: number;
}


export function getEnumerator(json: Object): Enumerator {
const enumerator = new Enumerator();
enumerator.displaytext = json["displaytext"] as string;
enumerator.shorttext = json["shorttext"] as string;
enumerator.parentCategory = json["parentCategory"] as string;
enumerator.enumkey_id = json["enumkey_id"] as number;
enumerator.enumvalue_id = json["enumvalue_id"] as number;
if (json.hasOwnProperty("id")) enumerator.id = json["id"] as number;
return enumerator;
}
11 changes: 11 additions & 0 deletions tests/components/MultiSelect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, it, expect } from "vitest";

import { mount } from "@vue/test-utils";
import MultiSelect from "../../components/MultiSelect.vue";

describe("MultiSelect", () => {
it("Title renders properly", () => {
const wrapper = mount(MultiSelect, { props: { title: "ATitle" } });
expect(wrapper.html()).toContain("ATitle");
});
});
9 changes: 9 additions & 0 deletions tests/components/vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import vue from '@vitejs/plugin-vue';

export default {
plugins: [vue()],
test: {
globals: true,
environment: 'jsdom',
},
}
73 changes: 73 additions & 0 deletions tests/integration/initdb.blocked.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { describe, expect, it, afterEach, beforeEach, beforeAll } from "vitest";
import fs from "fs";
import Enumerator from "../../server/db/entities/Enumerator";
import getDataSource, { setTestDataSource } from "../../server/db/dbsigleton";
import { DataSource } from "typeorm";

// Throws the following error and is therefore disabled
//
// FAIL tests/integration/initdb.test.ts [ tests/integration/initdb.test.ts ]
// TypeError: Class extends value undefined is not a constructor or null
// ❯ Object.<anonymous> node_modules/typeorm/connection/Connection.js:12:39
// ❯ Object.<anonymous> src/index.ts:136:0
// ❯ Object.<anonymous> node_modules/typeorm/driver/sap/SapDriver.js:4:13
// ❯ Object.<anonymous> node_modules/typeorm/driver/DriverFactory.js:19:21
// ❯ Object.<anonymous> node_modules/typeorm/data-source/DataSource.js:9:25
// ❯ Object.<anonymous> node_modules/typeorm/connection/ConnectionManager.js:4:22
// ❯ Object.<anonymous> node_modules/typeorm/globals.js:7:29
// ❯ src/index.ts:9:0
// ❯ async /home/alex/src/podkashde/node_modules/typeorm/index.mjs:1:256
// ❯ async /home/alex/src/podkashde/server/db/entities/Enumerator.ts:12:31
// ❯ async /home/alex/src/podkashde/tests/integration/initdb.test.ts:5:31
//
// it seems there is a problem initializing typeorm
// Observations:
// - It seems to use SapDriver ???
// - It happens at Enumerator entity - even when removing BaseEntity as base class
// - It does not depend on where I init the Datasource
// - It does not depend on refelct-metadata
// - It worked in previous version
// - Articles indicate that the error has to do with circular refs (but it works in prod)

export const dbTestFileName = "test.sqlite"
export const TstDataSource = new DataSource({
type: "sqlite",
database: dbTestFileName,
entities: [Enumerator],
logging: true,
synchronize: true,
});

describe("init database", async () => {
beforeAll(()=>{
setTestDataSource(TstDataSource);
})
beforeEach(()=>{
const existsBefore = fs.existsSync(dbTestFileName);
expect(existsBefore).toBeFalsy();
})
afterEach(async () => {
if (fs.existsSync(dbTestFileName)) fs.unlinkSync(dbTestFileName);
});

it("db file does not exist before init but after", async () => {
const db = await getDataSource();
const existsAfter = fs.existsSync(dbTestFileName);
expect(existsAfter).toBeTruthy();
});

it("initialized db file contains Enum Language de-DE", async () => {
const db = await getDataSource();
const german = await db.manager.findOneBy(Enumerator, {
shorttext: "de-DE",
});
expect(german).toBeTruthy();
});

it("initialize db file twice contains Enum Language de-DE only once", async () => {
var db = await getDataSource();
const repo = db.getRepository(Enumerator);
const german = await repo.find({ where: { shorttext: "de-DE" } });
expect(german.length).toBe(1);
});
});
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"types": ["@types/node", "vitest/importMeta"], //jest and vue??
}
}

0 comments on commit 9e562cf

Please sign in to comment.