Skip to content

Commit

Permalink
Add missing types for tests and npm packages
Browse files Browse the repository at this point in the history
Also bump version of `contentful-management` to a version that includes types.
  • Loading branch information
Peter Wielander committed Oct 12, 2020
1 parent aad3d32 commit 435ea6d
Show file tree
Hide file tree
Showing 47 changed files with 230 additions and 165 deletions.
3 changes: 1 addition & 2 deletions lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const DEFAULT_API_PRODUCERS = [
makeWindowAPI
]

// TODO; typing for return value
type producerFunc = (chanell: Channel, data: any, currentWindow: Window) => any
type producerFunc = (channel: Channel, data: any, currentWindow: Window) => any
const LOCATION_TO_API_PRODUCERS: { [location: string]: producerFunc[] } = {
[locations.LOCATION_ENTRY_FIELD]: DEFAULT_API_PRODUCERS,
[locations.LOCATION_ENTRY_FIELD_SIDEBAR]: DEFAULT_API_PRODUCERS,
Expand Down
1 change: 0 additions & 1 deletion lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const handleHandlerError = (err: Error) => {
return Promise.resolve(false)
}

// TODO any
const runHandler = (handler: Function, defaultResult: any, handlerArg?: any) => {
// Handler was not registered. Registering a handler is not
// required. We resolve with the default provided in this case.
Expand Down
2 changes: 1 addition & 1 deletion lib/channel.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Signal } from './signal'

// TODO derive arg1 and arg2 shape of onConnect callback
// TODO Both come through initial event.data over Channel
export default function connect(
currentWindow: Window,
onConnect: (arg1: Channel, args2: any, arg3: any) => void
) {
// TODO derive params and messageQueue shape from waitForConnect callback
waitForConnect(currentWindow, (params: any, messageQueue: any) => {
const channel = new Channel(params.id, currentWindow)
onConnect(channel, params, messageQueue)
Expand Down
15 changes: 11 additions & 4 deletions lib/entry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Channel } from './channel'
import { MemoizedSignal } from './signal'
import { EntryFieldInfo } from './types'

export default function createEntry(channel, entryData, fieldInfo, createEntryField) {
export default function createEntry(
channel: Channel,
entryData: any,
fieldInfo: EntryFieldInfo[],
createEntryField: Function
) {
let sys = entryData.sys
const sysChanged = new MemoizedSignal(sys)
const entryMetadata = entryData.metadata

channel.addHandler('sysChanged', _sys => {
channel.addHandler('sysChanged', (_sys: any) => {
sys = _sys
sysChanged.dispatch(sys)
})
Expand All @@ -14,10 +21,10 @@ export default function createEntry(channel, entryData, fieldInfo, createEntryFi
getSys() {
return sys
},
onSysChanged(handler) {
onSysChanged(handler: Function) {
return sysChanged.attach(handler)
},
fields: fieldInfo.reduce((acc, info) => {
fields: fieldInfo.reduce((acc: any, info: EntryFieldInfo) => {
acc[info.id] = createEntryField(info)
return acc
}, {}),
Expand Down
4 changes: 2 additions & 2 deletions lib/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default class Field implements EntryFieldAPI {
return this.setValue(undefined, locale)
}

// TODO swap argument positions to get rid of swap logic (breaking change)
// TODO maybe swap argument positions to get rid of swap logic
onValueChanged(locale: string | Function, handler?: Function) {
const h = handler || locale
if (!handler) {
Expand All @@ -63,7 +63,7 @@ export default class Field implements EntryFieldAPI {
return this._getFieldLocale(locale as string).onValueChanged(h as any)
}

// TODO swap argument positions to get rid of swap logic (breaking change)
// TODO maybe swap argument positions to get rid of swap logic
onIsDisabledChanged(locale: string | Function, handler?: Function) {
const h = handler || locale
if (!handler) {
Expand Down
12 changes: 4 additions & 8 deletions lib/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,13 @@ export default function createInitializer(
}
}

// TODO I have a feeling this could be simpler
function createDeferred<T = any>() {
const deferred: {
// TODO Types
// promise: Promise<T>
// resolve: ((value: T | PromiseLike<T>) => void)
promise: any
resolve: any
promise: Promise<T>
resolve: (value: T | PromiseLike<T>) => void
} = {
promise: null,
resolve: null
promise: null as any,
resolve: null as any
}

deferred.promise = new Promise<T>(resolve => {
Expand Down
2 changes: 0 additions & 2 deletions lib/navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import { Signal } from './signal'
export default function createNavigator(channel: Channel, ids: IdsAPI): NavigatorAPI {
const _onSlideInSignal = new Signal()

// TODO data shape
channel.addHandler('navigateSlideIn', (data: any) => {
_onSlideInSignal.dispatch(data)
})

// TODO return Promise casting
return {
openEntry: (id, opts) => {
return channel.call('navigateToContentEntity', {
Expand Down
1 change: 0 additions & 1 deletion lib/space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export default function createSpace(channel: Channel, initialContentTypes: Conte
const space = {} as SpaceAPI

spaceMethods.forEach(methodName => {
// TODO remove use of "any" here
space[methodName] = function(...args: any[]) {
return channel.call('callSpaceMethod', methodName, args)
} as any
Expand Down
122 changes: 89 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@
"@rollup/plugin-typescript": "^5.0.2",
"@testing-library/cypress": "5.3.0",
"@testing-library/dom": "6.12.2",
"@types/async-retry": "^1.4.2",
"@types/chai-as-promised": "^7.1.2",
"@types/cross-spawn": "^6.0.2",
"@types/fs-extra": "^9.0.2",
"@types/jsdom": "^16.2.1",
"@types/nanoid": "^2.1.0",
"@typescript-eslint/eslint-plugin": "^2.28.0",
"@typescript-eslint/parser": "^2.28.0",
"async-retry": "1.2.3",
"babel-eslint": "10.0.1",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"contentful-management": "5.7.2",
"contentful-management": "^6.1.2",
"cross-spawn": "6.0.5",
"cypress": "^4.4.1",
"cypress-plugin-retries": "^1.5.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import entryData from '../fixtures/entry-data.json'

export function verifySdkEntryData(iframeSelector) {
export function verifySdkEntryData(iframeSelector: string) {
cy.getSdk(iframeSelector).then(sdk => {
const keys = Object.keys(sdk.entry.fields)
keys.forEach(key => {
;['id', 'locales', 'type', 'required', 'validations'].forEach(property => {
expect(sdk.entry.fields[key][property]).deep.equal(entryData.fields[key][property])
expect(sdk.entry.fields[key][property]).deep.equal((entryData as any).fields[key][property])
})
})
})
}

export function checkSdkEntryDataTest(iframeSelector) {
export function checkSdkEntryDataTest(iframeSelector: string) {
it('sdk.entry static methods have expected values', () => {
verifySdkEntryData(iframeSelector)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Constants from '../../../constants'

export function checkSdkNavigationSlideInCallbackTest(iframeSelector) {
export function checkSdkNavigationSlideInCallbackTest(iframeSelector: any) {
it('sdk.navigator.onSlideInNavigation is called when slide-in editor is opened and when it is closed', () => {
cy.getSdk(iframeSelector).then(async sdk => {
const spy = cy.stub()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import cachedContentTypes from '../fixtures/cached-content-types'

export function verifySpaceGetCachedContentTypes(iframeSelector) {
export function verifySpaceGetCachedContentTypes(iframeSelector: string) {
cy.getSdk(iframeSelector).then(sdk => {
expect(sdk.space.getCachedContentTypes()).to.deep.equal(cachedContentTypes)
})
}

export function checkSdkSpaceMethods(iframeSelector) {
export function checkSdkSpaceMethods(iframeSelector: string) {
it('sdk.space.getCachedContentTypes returns an expected value', () => {
verifySpaceGetCachedContentTypes(iframeSelector)
})
Expand Down
Loading

0 comments on commit 435ea6d

Please sign in to comment.