2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-25 10:00:33 +03:00

chore: improve types

test: convert tests to ts
This commit is contained in:
pimlie
2021-04-05 01:01:28 +02:00
parent 8e2fed1525
commit 9d62122d22
14 changed files with 139 additions and 110 deletions
+3 -6
View File
@@ -6,10 +6,6 @@ module.exports = {
forceExit: false, forceExit: false,
// https://github.com/facebook/jest/pull/6747 fix warning here
// But its performance overhead is pretty bad (30+%).
// detectOpenHandles: true
setupFilesAfterEnv: ['./test/utils/setup'], setupFilesAfterEnv: ['./test/utils/setup'],
coverageDirectory: './coverage', coverageDirectory: './coverage',
@@ -31,8 +27,9 @@ module.exports = {
], ],
transform: { transform: {
'^.+\\.[tj]s$': 'babel-jest', '^.+\\.jsx?$': 'babel-jest',
'.*\\.(vue)$': 'vue-jest' '^.+\\.tsx?$': 'ts-jest',
'^.+\\.vue$': 'vue-jest'
}, },
moduleFileExtensions: [ moduleFileExtensions: [
+2 -1
View File
@@ -61,6 +61,7 @@
"@rollup/plugin-commonjs": "^17.1.0", "@rollup/plugin-commonjs": "^17.1.0",
"@rollup/plugin-node-resolve": "^11.2.0", "@rollup/plugin-node-resolve": "^11.2.0",
"@rollup/plugin-replace": "^2.4.1", "@rollup/plugin-replace": "^2.4.1",
"@types/jest": "^26.0.22",
"@types/webpack": "^4.41.26", "@types/webpack": "^4.41.26",
"@types/webpack-env": "^1.16.0", "@types/webpack-env": "^1.16.0",
"@typescript-eslint/eslint-plugin": "^4.15.2", "@typescript-eslint/eslint-plugin": "^4.15.2",
@@ -100,7 +101,7 @@
"selenium-webdriver": "^4.0.0-beta.1", "selenium-webdriver": "^4.0.0-beta.1",
"standard-version": "^9.1.1", "standard-version": "^9.1.1",
"tib": "^0.7.5", "tib": "^0.7.5",
"ts-jest": "^26.5.2", "ts-jest": "^26.5.4",
"ts-loader": "^8.0.17", "ts-loader": "^8.0.17",
"typescript": "^4.2.2", "typescript": "^4.2.2",
"vite": "^2.0.4", "vite": "^2.0.4",
+3 -3
View File
@@ -60,12 +60,12 @@ export const createMetaManager = (config?: MetaConfig, resolver?: MetaResolver):
export class MetaManager { export class MetaManager {
config: MetaConfig config: MetaConfig
target: MergedObjectBuilder target: MergedObjectBuilder<MetaSource>
resolver?: MetaResolverSetup resolver?: MetaResolverSetup
ssrCleanedUp: boolean = false ssrCleanedUp: boolean = false
constructor (config: MetaConfig, target: MergedObjectBuilder, resolver: MetaResolver | ResolveMethod) { constructor (config: MetaConfig, target: MergedObjectBuilder<MetaSource>, resolver: MetaResolver | ResolveMethod) {
this.config = config this.config = config
this.target = target this.target = target
@@ -83,7 +83,7 @@ export class MetaManager {
return resolver.resolve(options, contexts, active, key, pathSegments) return resolver.resolve(options, contexts, active, key, pathSegments)
} }
const mergedObject = createMergedObject(resolve, active) const mergedObject = createMergedObject<MetaSource>(resolve, active)
// TODO: validate resolver // TODO: validate resolver
const manager = new MetaManager(config, mergedObject, resolver) const manager = new MetaManager(config, mergedObject, resolver)
+4 -4
View File
@@ -1,5 +1,5 @@
// https://github.com/microsoft/TypeScript/issues/1863 // https://github.com/microsoft/TypeScript/issues/1863
export const IS_PROXY = Symbol('kIsProxy') as unknown as string export const IS_PROXY = Symbol('kIsProxy')
export const PROXY_SOURCES = Symbol('kProxySources') as unknown as string export const PROXY_SOURCES = Symbol('kProxySources')
export const PROXY_TARGET = Symbol('kProxyTarget') as unknown as string export const PROXY_TARGET = Symbol('kProxyTarget')
export const RESOLVE_CONTEXT = Symbol('kResolveContext') as unknown as string export const RESOLVE_CONTEXT = Symbol('kResolveContext')
+26 -25
View File
@@ -1,9 +1,14 @@
import { PROXY_TARGET } from './constants' import { IS_PROXY, PROXY_SOURCES, PROXY_TARGET, RESOLVE_CONTEXT } from './constants'
import { createProxy } from './proxy' import { createProxy } from './proxy'
import { recompute } from './recompute' import { recompute } from './recompute'
export type MergeSource = { export interface ResolveContext {}
[key: string]: any
export type MergeSource<T extends Object> = { [K in keyof T]: T[K] } & {
[IS_PROXY]: boolean
[PROXY_SOURCES]: MergeSource<T>[]
[PROXY_TARGET]: MergeSource<T>
[RESOLVE_CONTEXT]: ResolveContext
} }
// eslint-disable-next-line no-use-before-define // eslint-disable-next-line no-use-before-define
@@ -15,49 +20,45 @@ export type MergedObject = {
export type PathSegments = Array<string> export type PathSegments = Array<string>
export type ResolveContext = {} export interface ResolveMethod<T = any, U = ResolveContext> {
(
export type ResolveMethod<T = ResolveContext> = ( options: Array<T>,
options: Array<any>, contexts: Array<U>,
contexts: Array<T>,
active: MergedObjectValue, active: MergedObjectValue,
key: string | number | symbol, key: string | number | symbol,
pathSegments: PathSegments, pathSegments: PathSegments,
) => MergedObjectValue ): MergedObjectValue
}
export type MergeContext = { export type MergeContext<T> = {
resolve: ResolveMethod resolve: ResolveMethod
active: MergedObject active: MergedObject
sources: Array<MergeSource> sources: MergeSource<T>[]
} }
export type MergedObjectBuilder = { export type MergedObjectBuilder<T> = {
context: MergeContext context: MergeContext<T>
compute: () => void compute: () => void
addSource: (source: MergeSource, resolveContext: ResolveContext | undefined, recompute?: Boolean) => any addSource: (source: T, resolveContext?: ResolveContext, recompute?: Boolean) => any
delSource: (sourceOrProxy: MergeSource, recompute?: boolean) => boolean delSource: (sourceOrProxy: T | MergeSource<T>, recompute?: boolean) => boolean
} }
export const createMergedObject = (resolve: ResolveMethod, active: MergedObject = {}): MergedObjectBuilder => { export const createMergedObject = <T extends Object>(resolve: ResolveMethod<T>, active: T): MergedObjectBuilder<T> => {
const sources: Array<MergeSource> = [] const sources: MergeSource<T>[] = []
if (!active) { const context: MergeContext<T> = {
active = {}
}
const context: MergeContext = {
active, active,
resolve, resolve,
sources sources
} }
const compute: () => void = () => recompute(context) const compute: () => void = () => recompute<T>(context)
return { return {
context, context,
compute, compute,
addSource: (source, resolveContext, recompute = false) => { addSource: (source, resolveContext, recompute = false) => {
const proxy = createProxy(context, source, resolveContext || {}) const proxy = createProxy<T>(context, source, resolveContext || {})
if (recompute) { if (recompute) {
compute() compute()
@@ -66,7 +67,7 @@ export const createMergedObject = (resolve: ResolveMethod, active: MergedObject
return proxy return proxy
}, },
delSource: (sourceOrProxy, recompute = true) => { delSource: (sourceOrProxy, recompute = true) => {
const index = sources.findIndex(src => src === sourceOrProxy || src[PROXY_TARGET] === sourceOrProxy) const index = sources.findIndex(source => source === sourceOrProxy || source[PROXY_TARGET] === sourceOrProxy)
if (index > -1) { if (index > -1) {
sources.splice(index, 1) sources.splice(index, 1)
+11 -10
View File
@@ -5,9 +5,9 @@ import { IS_PROXY, PROXY_SOURCES, PROXY_TARGET, RESOLVE_CONTEXT } from './consta
import { recompute } from './recompute' import { recompute } from './recompute'
import type { MergeContext, MergeSource, MergedObjectValue, PathSegments, ResolveContext } from '.' import type { MergeContext, MergeSource, MergedObjectValue, PathSegments, ResolveContext } from '.'
export const createProxy = (context: MergeContext, target: MergeSource, resolveContext: ResolveContext, pathSegments: PathSegments = []) => { export const createProxy = <T extends Record<string, any>>(context: MergeContext<T>, target: T, resolveContext: ResolveContext, pathSegments: PathSegments = []): MergeSource<T> => {
const handler = createHandler(context, resolveContext, pathSegments) const handler = createHandler<T>(context, resolveContext, pathSegments)
const proxy = markRaw(new Proxy(target, handler)) const proxy = markRaw(new Proxy(target, handler)) as MergeSource<T>
if (!pathSegments.length && context.sources) { if (!pathSegments.length && context.sources) {
context.sources.push(proxy) context.sources.push(proxy)
@@ -16,7 +16,7 @@ export const createProxy = (context: MergeContext, target: MergeSource, resolveC
return proxy return proxy
} }
export const createHandler: (context: MergeContext, resolveContext: ResolveContext, pathSegments: PathSegments) => ProxyHandler<any> = (context, resolveContext, pathSegments = []) => ({ export const createHandler = <T>(context: MergeContext<T>, resolveContext: ResolveContext, pathSegments: PathSegments = []): ProxyHandler<MergeSource<T>> => ({
get: (target, key, receiver) => { get: (target, key, receiver) => {
if (key === IS_PROXY) { if (key === IS_PROXY) {
return true return true
@@ -40,11 +40,11 @@ export const createHandler: (context: MergeContext, resolveContext: ResolveConte
return value return value
} }
if (!value[IS_PROXY]) { if (!(value as MergeSource<T>)[IS_PROXY]) {
const keyPath: PathSegments = [...pathSegments, (key as string)] const keyPath: PathSegments = [...pathSegments, (key as string)]
value = createProxy(context, value, resolveContext, keyPath) value = createProxy<typeof value>(context, value, resolveContext, keyPath)
target[key] = value Reflect.set(target, key, value)
} }
return value return value
@@ -87,8 +87,8 @@ export const createHandler: (context: MergeContext, resolveContext: ResolveConte
return success return success
} }
let keyContexts: Array<ResolveContext> = [] let keyContexts: ResolveContext[] = []
let keySources let keySources: MergeSource<T>[]
if (isArrayItem) { if (isArrayItem) {
keySources = proxies keySources = proxies
@@ -138,6 +138,7 @@ export const createHandler: (context: MergeContext, resolveContext: ResolveConte
let index = 0 let index = 0
for (const segment of pathSegments) { for (const segment of pathSegments) {
// @ts-ignore
proxies = proxies.map(proxy => proxy[segment]) proxies = proxies.map(proxy => proxy[segment])
if (isArrayItem && index === pathSegments.length - 1) { if (isArrayItem && index === pathSegments.length - 1) {
@@ -152,7 +153,7 @@ export const createHandler: (context: MergeContext, resolveContext: ResolveConte
// Check if the key still exists in one of the sourceProxies, // Check if the key still exists in one of the sourceProxies,
// if so resolve the new value, if not remove the key // if so resolve the new value, if not remove the key
if (proxies.some(proxy => (key in proxy))) { if (proxies.some(proxy => (key in proxy))) {
let keyContexts: Array<ResolveContext> = [] let keyContexts: ResolveContext[] = []
let keySources let keySources
if (isArrayItem) { if (isArrayItem) {
+5 -2
View File
@@ -3,7 +3,7 @@ import { clone, pluck } from '../utils'
import { RESOLVE_CONTEXT } from './constants' import { RESOLVE_CONTEXT } from './constants'
import type { MergeContext, MergeSource, MergedObject, PathSegments, ResolveContext } from '.' import type { MergeContext, MergeSource, MergedObject, PathSegments, ResolveContext } from '.'
export const allKeys = (source?: MergeSource, ...sources: Array<MergeSource>): Array<string> => { export const allKeys = <T>(source?: MergeSource<T>, ...sources: MergeSource<T>[]): string[] => {
const keys = source ? Object.keys(source) : [] const keys = source ? Object.keys(source) : []
if (sources) { if (sources) {
@@ -25,7 +25,7 @@ export const allKeys = (source?: MergeSource, ...sources: Array<MergeSource>): A
return keys return keys
} }
export const recompute = (context: MergeContext, sources?: Array<MergeSource>, target?: MergedObject, path: PathSegments = []): void => { export const recompute = <T>(context: MergeContext<T>, sources?: MergeSource<T>[], target?: MergedObject, path: PathSegments = []): void => {
if (!path.length) { if (!path.length) {
if (!target) { if (!target) {
target = context.active target = context.active
@@ -52,6 +52,7 @@ export const recompute = (context: MergeContext, sources?: Array<MergeSource>, t
for (const key of keys) { for (const key of keys) {
// This assumes consistent types usages for keys across sources // This assumes consistent types usages for keys across sources
// @ts-ignore
if (isPlainObject(sources[0][key])) { if (isPlainObject(sources[0][key])) {
if (!target[key]) { if (!target[key]) {
target[key] = {} target[key] = {}
@@ -60,6 +61,7 @@ export const recompute = (context: MergeContext, sources?: Array<MergeSource>, t
const keySources = [] const keySources = []
for (const source of sources) { for (const source of sources) {
if (key in source) { if (key in source) {
// @ts-ignore
keySources.push(source[key]) keySources.push(source[key])
} }
} }
@@ -69,6 +71,7 @@ export const recompute = (context: MergeContext, sources?: Array<MergeSource>, t
} }
// Ensure the target is an array if source is an array and target is empty // Ensure the target is an array if source is an array and target is empty
// @ts-ignore
if (!target[key] && isArray(sources[0][key])) { if (!target[key] && isArray(sources[0][key])) {
target[key] = [] target[key] = []
} }
+1 -1
View File
@@ -4,7 +4,7 @@ export interface ResolveOptionPredicament<T, U> {
(currentValue: T | undefined, context: U): T (currentValue: T | undefined, context: U): T
} }
export const resolveOption = <T, U = ResolveContext>(predicament: ResolveOptionPredicament<T, U>, initialValue?: T): ResolveMethod<U> => (options, contexts) => { export const resolveOption = <T, U = ResolveContext>(predicament: ResolveOptionPredicament<T, U>, initialValue?: T): ResolveMethod<any, U> => (options, contexts) => {
let resolvedIndex = -1 let resolvedIndex = -1
contexts.reduce((acc, context, index) => { contexts.reduce((acc, context, index) => {
+2 -2
View File
@@ -1,6 +1,6 @@
export const pluck = (collection: Array<any>, key: string, callback?: (row: any) => void) => { export const pluck = <T extends Record<string, any>>(collection: T[], key: string, callback?: (row: T) => void) => {
const plucked: Array<any> = [] const plucked: T[] = []
for (const row of collection) { for (const row of collection) {
if (key in row) { if (key in row) {
@@ -1,7 +1,9 @@
import { MergeContext } from 'src/object-merge'
import { createProxy } from '../../src/object-merge/proxy' import { createProxy } from '../../src/object-merge/proxy'
describe('proxy', () => { describe('proxy', () => {
let context let context: MergeContext<any>
beforeEach(() => { beforeEach(() => {
context = { context = {
sources: [], sources: [],
@@ -18,7 +20,7 @@ describe('proxy', () => {
} }
} }
const proxy = createProxy(context, target) const proxy = createProxy(context, target, {})
expect(proxy.str).toBe('test') expect(proxy.str).toBe('test')
expect(proxy.obj.str).toBe('test') expect(proxy.obj.str).toBe('test')
@@ -27,7 +29,7 @@ describe('proxy', () => {
test('string (set, update, delete)', () => { test('string (set, update, delete)', () => {
const target = {} const target = {}
const proxy = createProxy(context, target) const proxy = createProxy(context, target, {})
proxy.str = 'test' proxy.str = 'test'
@@ -45,7 +47,7 @@ describe('proxy', () => {
test('array (set, update, delete)', () => { test('array (set, update, delete)', () => {
const target = {} const target = {}
const proxy = createProxy(context, target) const proxy = createProxy(context, target, {})
proxy.arr = [0, 1] proxy.arr = [0, 1]
@@ -68,7 +70,7 @@ describe('proxy', () => {
test('proxy (set object)', () => { test('proxy (set object)', () => {
const target = {} const target = {}
const proxy = createProxy(context, target) const proxy = createProxy(context, target, {})
proxy.obj = { str: 'test' } proxy.obj = { str: 'test' }
@@ -79,7 +81,7 @@ describe('proxy', () => {
test('proxy (remove)', () => { test('proxy (remove)', () => {
const target = {} const target = {}
const proxy = createProxy(context, target) const proxy = createProxy(context, target, {})
proxy.obj = { str: 'test' } proxy.obj = { str: 'test' }
@@ -93,7 +95,7 @@ describe('proxy', () => {
test('proxy (remove child)', () => { test('proxy (remove child)', () => {
const target = {} const target = {}
const proxy = createProxy(context, target) const proxy = createProxy(context, target, {})
proxy.obj = { str: 'test' } proxy.obj = { str: 'test' }
@@ -1,10 +1,12 @@
// @ts-nocheck
import { MetaRenderContext } from 'src'
import * as render from '../../src/render' import * as render from '../../src/render'
// Note: testing isnt rly independent as they also rely on ./src/config/tags // Note: testing isnt rly independent as they also rely on ./src/config/tags
describe('render', () => { describe('render', () => {
test('render key-string element (without value attribute)', () => { test('render key-string element (without value attribute)', () => {
const context = {} const context: MetaRenderContext = { metainfo: {} }
const key = 'TitleTest' const key = 'TitleTest'
const data = 'my title' const data = 'my title'
const config = { const config = {
@@ -13,6 +15,10 @@ describe('render', () => {
const res = render.renderMeta(context, key, data, config) const res = render.renderMeta(context, key, data, config)
// console.log('RES', res) // console.log('RES', res)
if (!res) {
expect(res).toBeTruthy()
return
}
expect(res.to).toBeUndefined() expect(res.to).toBeUndefined()
expect(res.vnode).toMatchObject({ __v_isVNode: true }) expect(res.vnode).toMatchObject({ __v_isVNode: true })
@@ -24,7 +30,7 @@ describe('render', () => {
}) })
test('render key-string element (without name attribute)', () => { test('render key-string element (without name attribute)', () => {
const context = {} const context: MetaRenderContext = { metainfo: {} }
const key = 'CharsetTest' const key = 'CharsetTest'
const data = 'utf8' const data = 'utf8'
const config = { const config = {
@@ -44,7 +50,7 @@ describe('render', () => {
}) })
test('render key-string element (with name attribute)', () => { test('render key-string element (with name attribute)', () => {
const context = {} const context: MetaRenderContext = { metainfo: {} }
const key = 'DescriptionTest' const key = 'DescriptionTest'
const data = 'my description' const data = 'my description'
const config = { const config = {
@@ -62,7 +68,7 @@ describe('render', () => {
}) })
test('render key-object element', () => { test('render key-object element', () => {
const context = {} const context: MetaRenderContext = { metainfo: {} }
const key = 'DescriptionTest2' const key = 'DescriptionTest2'
const data = { content: 'my description 2' } const data = { content: 'my description 2' }
const config = { const config = {
@@ -80,7 +86,7 @@ describe('render', () => {
}) })
test('render key-object element with json', () => { test('render key-object element with json', () => {
const context = {} const context: MetaRenderContext = { metainfo: {} }
const key = 'JsonTest' const key = 'JsonTest'
const data = { json: ['content'] } const data = { json: ['content'] }
const config = { const config = {
@@ -99,7 +105,7 @@ describe('render', () => {
}) })
test('render key-object element with raw content', () => { test('render key-object element with raw content', () => {
const context = {} const context: MetaRenderContext = { metainfo: {} }
const key = 'RawTest' const key = 'RawTest'
const data = { rawContent: '<p>One JS please!</p>' } const data = { rawContent: '<p>One JS please!</p>' }
const config = { const config = {
@@ -117,7 +123,7 @@ describe('render', () => {
}) })
test('render array<key-string> elements', () => { test('render array<key-string> elements', () => {
const context = {} const context: MetaRenderContext = { metainfo: {} }
const key = 'kal-el' const key = 'kal-el'
const data = [ const data = [
'man', 'man',
@@ -146,7 +152,7 @@ describe('render', () => {
}) })
test('render array<key-object> elements', () => { test('render array<key-object> elements', () => {
const context = {} const context: MetaRenderContext = { metainfo: {} }
const key = 'kal-el' const key = 'kal-el'
const data = [ const data = [
{ super: 'man' }, { super: 'man' },
@@ -173,7 +179,7 @@ describe('render', () => {
}) })
test('render custom group', () => { test('render custom group', () => {
const context = {} const context: MetaRenderContext = { metainfo: {} }
const key = 'customGroup' const key = 'customGroup'
const data = { const data = {
title: 'my custom title', title: 'my custom title',
@@ -203,7 +209,7 @@ describe('render', () => {
}) })
test('render custom group (namespaced tag)', () => { test('render custom group (namespaced tag)', () => {
const context = {} const context: MetaRenderContext = { metainfo: {} }
const key = 'og' const key = 'og'
const data = { const data = {
title: 'my og title', title: 'my og title',
@@ -249,7 +255,7 @@ describe('render', () => {
}) })
test('render custom group (namespaced attribute and name attribute)', () => { test('render custom group (namespaced attribute and name attribute)', () => {
const context = {} const context: MetaRenderContext = { metainfo: {} }
const key = 'og' const key = 'og'
const data = { const data = {
title: 'my og title', title: 'my og title',
@@ -296,7 +302,7 @@ describe('render', () => {
test('render custom group (array)', () => { test('render custom group (array)', () => {
const spy = jest.spyOn(console, 'warn').mockImplementation(_ => _) const spy = jest.spyOn(console, 'warn').mockImplementation(_ => _)
const context = {} const context: MetaRenderContext = { metainfo: {} }
const key = 'og' const key = 'og'
const data = ['data'] const data = ['data']
@@ -319,7 +325,7 @@ describe('render', () => {
}) })
test('render custom group (tag namespaced)', () => { test('render custom group (tag namespaced)', () => {
const context = {} const context: MetaRenderContext = { metainfo: {} }
const key = 'esi' const key = 'esi'
const data = { const data = {
children: [{ children: [{
@@ -483,7 +489,7 @@ describe('render', () => {
removeAttribute removeAttribute
}]) }])
const context = {} const context: MetaRenderContext = { metainfo: {} }
const res = render.renderMeta(context, key, data, config) const res = render.renderMeta(context, key, data, config)
// console.log('RES', res) // console.log('RES', res)
@@ -1,19 +1,20 @@
import { isArray, isPlainObject } from '@vue/shared' import { isArray, isPlainObject } from '@vue/shared'
import { createMergedObject } from '../../src/object-merge' import { createMergedObject, ResolveMethod } from '../../src/object-merge'
const resolve = (options) => { const resolve: ResolveMethod = <T>(sources: T[]) => {
// console.log('RESOLVE\n', options) // console.log('RESOLVE\n', sources)
const hasArrayOption = options.some(option => isArray(option)) const hasArrayOption = sources.some(source => isArray(source))
if (hasArrayOption) { if (hasArrayOption) {
const groupedOptions = {} const groupedOptions: Record<string, object> = {}
for (const option of options) { for (const source of sources) {
if (!isArray(option)) { if (!isArray(source)) {
continue continue
} }
for (const value of option) { for (const value of source) {
if (isPlainObject(value) && 'vmid' in value) { if (isPlainObject(value) && 'vmid' in value) {
// @ts-ignore
groupedOptions[value.vmid] = value groupedOptions[value.vmid] = value
} }
} }
@@ -21,16 +22,19 @@ const resolve = (options) => {
// console.log('GROUPED OPTIONS', groupedOptions) // console.log('GROUPED OPTIONS', groupedOptions)
const values = [] const values = []
for (const option of options) { for (const source of sources) {
if (!isArray(option)) { if (!isArray(source)) {
continue continue
} }
for (const value of option) { for (const value of source) {
if (!isPlainObject(value) || !('vmid' in value)) { if (!isPlainObject(value) || !('vmid' in value)) {
values.push(value) values.push(value)
// @ts-ignore
} else if (groupedOptions[value.vmid]) { } else if (groupedOptions[value.vmid]) {
// @ts-ignore
values.push(groupedOptions[value.vmid]) values.push(groupedOptions[value.vmid])
// @ts-ignore
delete groupedOptions[value.vmid] delete groupedOptions[value.vmid]
} }
} }
@@ -40,7 +44,7 @@ const resolve = (options) => {
return values return values
} }
return options[options.length - 1] return sources[sources.length - 1]
} }
describe('resolve', () => { describe('resolve', () => {
@@ -53,11 +57,14 @@ describe('resolve', () => {
str: 'string value 2' str: 'string value 2'
} }
const { active, addSource, delSource } = createMergedObject(resolve) type Source = { str?: string }
const { context, addSource, delSource } = createMergedObject<Source>(resolve, {})
const { active } = context
// Set initial value & init proxy // Set initial value & init proxy
addSource(source1) addSource(source1)
const proxy2 = addSource(source2, null, true /* do an initial compute/walk of all sources */) const proxy2 = addSource(source2, undefined, true /* do an initial compute/walk of all sources */)
expect(active.str).toBe('string value 2') expect(active.str).toBe('string value 2')
@@ -87,11 +94,14 @@ describe('resolve', () => {
} }
} }
const { active, addSource, delSource } = createMergedObject(resolve) type Source = { obj?: { key?: string } }
const { context, addSource, delSource } = createMergedObject<Source>(resolve, {})
const { active } = context
// Set initial value & init proxy // Set initial value & init proxy
const proxy1 = addSource(source1) const proxy1 = addSource(source1)
const proxy2 = addSource(source2, null, true /* do an initial compute/walk of all sources */) const proxy2 = addSource(source2, undefined, true /* do an initial compute/walk of all sources */)
expect(active.obj.key).toBe('object value 2') expect(active.obj.key).toBe('object value 2')
@@ -126,11 +136,12 @@ describe('resolve', () => {
] ]
} }
const { active, sources, addSource, delSource } = createMergedObject(resolve) const { context, addSource, delSource } = createMergedObject(resolve, {})
const { active, sources } = context
// Set initial value & init proxy // Set initial value & init proxy
const proxy1 = addSource(source1) const proxy1 = addSource(source1)
const proxy2 = addSource(source2, null, true /* do an initial compute/walk of all sources */) const proxy2 = addSource(source2, undefined, true /* do an initial compute/walk of all sources */)
expect(active.arr).toEqual(['array value 1', 'array value 2']) expect(active.arr).toEqual(['array value 1', 'array value 2'])
@@ -156,7 +167,7 @@ describe('resolve', () => {
expect(active.arr).toEqual(['test again 2.1']) expect(active.arr).toEqual(['test again 2.1'])
proxy1.arr = ['test again 1'] proxy1.arr = ['test again 1']
addSource(proxy1, null, true) addSource(proxy1, undefined, true)
expect(active.arr).toEqual(['test again 2.1', 'test again 1']) expect(active.arr).toEqual(['test again 2.1', 'test again 1'])
proxy2.arr = [] proxy2.arr = []
@@ -179,11 +190,19 @@ describe('resolve', () => {
] ]
} }
const { active, sources, addSource, delSource } = createMergedObject(resolve) type Source = {
arr?: {
vmid?: string,
key: string
}[]
}
const { context, addSource, delSource } = createMergedObject<Source>(resolve, {})
const { active, sources } = context
// Set initial value & init proxy // Set initial value & init proxy
const proxy1 = addSource(source1) const proxy1 = addSource(source1)
const proxy2 = addSource(source2, null, true /* do an initial compute/walk of all sources */) const proxy2 = addSource(source2, undefined, true /* do an initial compute/walk of all sources */)
expect(active.arr).toEqual([ expect(active.arr).toEqual([
{ key: 'collection value 1.1' }, { key: 'collection value 1.1' },
@@ -227,7 +246,7 @@ describe('resolve', () => {
expect(active.arr).toBeUndefined() expect(active.arr).toBeUndefined()
expect(active).toEqual({}) expect(active).toEqual({})
const proxy3 = addSource({ arr: [{ vmid: 'a', key: 'test again 1' }] }, null, true) const proxy3 = addSource({ arr: [{ vmid: 'a', key: 'test again 1' }] }, undefined, true)
expect(sources.length).toBe(2) expect(sources.length).toBe(2)
expect(active.arr).toEqual([{ vmid: 'a', key: 'test again 1' }]) expect(active.arr).toEqual([{ vmid: 'a', key: 'test again 1' }])
+8 -9
View File
@@ -985,10 +985,10 @@
dependencies: dependencies:
"@types/istanbul-lib-report" "*" "@types/istanbul-lib-report" "*"
"@types/jest@26.x": "@types/jest@^26.0.22":
version "26.0.20" version "26.0.22"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-26.0.20.tgz#cd2f2702ecf69e86b586e1f5223a60e454056307" resolved "https://registry.yarnpkg.com/@types/jest/-/jest-26.0.22.tgz#8308a1debdf1b807aa47be2838acdcd91e88fbe6"
integrity sha512-9zi2Y+5USJRxd0FsahERhBwlcvFh6D2GLQnY2FH2BzK8J9s9omvNHIbvABwIluXa0fD8XVKMLTO0aOEuUfACAA== integrity sha512-eeWwWjlqxvBxc4oQdkueW5OF/gtfSceKk4OnOAGlUSwS/liBRtZppbJuz1YkgbrbfGOoeBHun9fOvXnjNwrSOw==
dependencies: dependencies:
jest-diff "^26.0.0" jest-diff "^26.0.0"
pretty-format "^26.0.0" pretty-format "^26.0.0"
@@ -9082,12 +9082,11 @@ trim-off-newlines@^1.0.0:
resolved "https://registry.yarnpkg.com/trim-off-newlines/-/trim-off-newlines-1.0.1.tgz#9f9ba9d9efa8764c387698bcbfeb2c848f11adb3" resolved "https://registry.yarnpkg.com/trim-off-newlines/-/trim-off-newlines-1.0.1.tgz#9f9ba9d9efa8764c387698bcbfeb2c848f11adb3"
integrity sha1-n5up2e+odkw4dpi8v+sshI8RrbM= integrity sha1-n5up2e+odkw4dpi8v+sshI8RrbM=
ts-jest@^26.5.2: ts-jest@^26.5.4:
version "26.5.2" version "26.5.4"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-26.5.2.tgz#5281d6b44c2f94f71205728a389edc3d7995b0c4" resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-26.5.4.tgz#207f4c114812a9c6d5746dd4d1cdf899eafc9686"
integrity sha512-bwyJ2zJieSugf7RB+o8fgkMeoMVMM2KPDE0UklRLuACxjwJsOrZNo6chrcScmK33YavPSwhARffy8dZx5LJdUQ== integrity sha512-I5Qsddo+VTm94SukBJ4cPimOoFZsYTeElR2xy6H2TOVs+NsvgYglW8KuQgKoApOKuaU/Ix/vrF9ebFZlb5D2Pg==
dependencies: dependencies:
"@types/jest" "26.x"
bs-logger "0.x" bs-logger "0.x"
buffer-from "1.x" buffer-from "1.x"
fast-json-stable-stringify "2.x" fast-json-stable-stringify "2.x"