Тесты работают, TS билдится

This commit is contained in:
Nikita Dezzpil Orlov committed 2021-09-29 17:11:49 +03:00
1 parent e08d9757bb
commit 9f3a64d76f
36 files changed
+17901

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
const Sequencer = require('@jest/test-sequencer').default;
class CustomSequencer extends Sequencer {
sort(tests) {
// Test structure information
// https://github.com/facebook/jest/blob/6b8b1404a1d9254e7d5d90a8934087a9c9899dab/packages/jest-runner/src/types.ts#L17-L21
const copyTests = Array.from(tests);
// @ts-ignore
return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1));
}
}
module.exports = CustomSequencer;
+10
View File
@@ -0,0 +1,10 @@
const failFast = require("jasmine-fail-fast");
// Добавляем нормальный fail fast
// https://github.com/facebook/jest/issues/2867
//if (process.argv.includes("--bail")) {
// @ts-ignore
const jasmineEnv = jasmine.getEnv();
jasmineEnv.addReporter(failFast.init());
//}
View File
Whitespace-only changes.
+48
View File
@@ -0,0 +1,48 @@
import assert from 'assert'
import { Data } from '../app/data'
export class ItemData extends Data {
uniqKey(): string {
return 'a'
}
a: number = 0
b: number
c: { d: number; e?: number } = { d: 1 }
__f: number = 0
sum() {
return this.a + this.c.d + this.c.e
}
}
describe('Data', function () {
describe('#toObject', function () {
it('should return object', function () {
const data = new ItemData()
data.a = 1
data.c = { d: 2, e: undefined }
const obj = data.toObject()
assert.strictEqual('a' in obj, true)
assert.strictEqual('b' in obj, false)
assert.strictEqual('c' in obj, true)
assert.strictEqual('d' in obj['c'], true)
assert.strictEqual('e' in obj['c'], true)
assert.strictEqual('__f' in obj, false)
assert.strictEqual('sum' in obj, false)
})
})
describe('#fromObject', () => {
it('should return data object', () => {
const dataOrig = new ItemData()
dataOrig.a = 1
dataOrig.c = { d: 2, e: undefined }
const obj = dataOrig.toObject()
const dataRecovered = new ItemData()
dataRecovered.fromObject(obj)
assert.notStrictEqual(dataOrig, dataRecovered)
})
})
})
+50
View File
@@ -0,0 +1,50 @@
import { ItemData } from './data.test'
import { Entity } from '../app/entity'
import { Storage } from '../app/storage'
import { Repo } from '../app/repo'
import { MapStorage } from './storage.test'
import { Data } from '../app/data'
import assert from 'assert'
class Item extends Entity<ItemData> {
_getVO(): ItemData {
return new ItemData()
}
protected _data: ItemData
getA(): number {
return this._data.a
}
getC(): { d: number; e?: number } {
return this._data.c
}
_getRepo(storage: Storage): ItemRepository {
return new ItemRepository(storage)
}
}
class ItemRepository extends Repo<Item> {
Name() {
return 'items'
}
Entity() {
return new Item()
}
}
describe('ItemRepository', function () {
describe('map storage save', function () {
const storage = new MapStorage('')
const repo = new ItemRepository(storage)
it('should return entity', async () => {
const dt = Data.assign(new ItemData(), { a: 1, b: 2, c: { d: 3 } })
const item = new Item(dt as ItemData)
await repo.save(item)
const itemFromRepo = await repo.findById('1')
assert.notStrictEqual(item.data, itemFromRepo.data)
})
})
})
+132
View File
@@ -0,0 +1,132 @@
import { strict as assert } from 'assert'
import { Storage, StorageCursor, StorageSession } from '../app/storage'
export class MapCursor implements StorageCursor {
_map: Map<any, any>
_limit = 0
constructor(map: Map<any, any>) {
this._map = map
}
limit(num: number): StorageCursor {
this._limit = num
return this
}
sort(params: {}): StorageCursor {
return this
}
toArray(): Promise<any[]> {
return Promise.resolve(Array.from(this._map.values()))
}
skip(offset: number): StorageCursor {
return this
}
}
export class MapStorage extends Storage {
private readonly _mapPool
private readonly _mapPoolId
constructor(dsn: string) {
super(dsn)
this._mapPoolId = {}
this._mapPool = {}
}
async find(name: string, query: Object): Promise<StorageCursor> {
if (!(name in this._mapPool)) {
return new MapCursor(new Map())
}
const map: Map<any, any> = new Map()
for (let [_, elem] of this._mapPool[name]) {
nextParam: for (let [key, val] of Object.entries(query)) {
if (key in elem) {
for (let symbol of ['=', '>', '<', '!']) {
if ((val + '').startsWith(symbol)) {
if (eval(elem[key] + val + '')) {
map.set(_, elem)
break nextParam
}
}
}
if (elem[key] == val) {
map.set(_, elem)
}
}
}
}
return new MapCursor(map)
}
count(name: string, query: object): Promise<number> {
return Promise.resolve(0)
}
remove(collectionName: string, uniqKeyName: string, uniqKey: string): Promise<boolean> {
return Promise.resolve(true)
}
async save(name: string, idKey: string, data: Object): Promise<string> {
if (!(name in this._mapPool)) {
this._mapPool[name] = new Map()
this._mapPoolId[name] = {}
}
const id = data[idKey] + ''
this._mapPool[name].set(id, data)
if (!(id in this._mapPoolId[name])) {
this._mapPoolId[name][id] = Date.now()
}
return Promise.resolve(this._mapPoolId[name][id])
}
createSession(): StorageSession {
return new MapStorageSession()
}
}
class MapStorageSession extends StorageSession {
async commit(fn, options?: {}): Promise<any> {
return Promise.resolve(undefined)
}
async start(options?: {}): Promise<any> {
return Promise.resolve(undefined)
}
}
describe('Storage', function () {
describe('#save', function () {
it('should return inner id', async () => {
const storage = new MapStorage('')
await storage.save('items', 'id', { id: 100, value: 'one' })
const _id2 = await storage.save('items', 'id', { id: 102, value: 'two' })
const _id3 = await storage.save('items', 'id', { id: 102, value: 'two' })
assert.equal(_id2, _id3)
})
})
describe('#find', () => {
it('should return cursor', async () => {
const storage = new MapStorage('')
await storage.save('items', 'id', { id: 1, value: 'one' })
await storage.save('items', 'id', { id: 2, value: 'two' })
await storage.save('elements', 'id', { id: 1, value: 2 })
await storage.save('elements', 'id', { id: 2, value: 11 })
await storage.save('elements', 'id', { id: 3, value: 12 })
let cursor
cursor = await storage.find('items', { value: 'one' })
assert.equal((await cursor.toArray()).length, 1)
cursor = await storage.find('elements', { value: '>10' })
assert.equal((await cursor.toArray()).length, 2)
})
})
})