Skip to content

Commit

Permalink
test(portable-text-editor): fix tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuslundgard committed Sep 28, 2022
1 parent b5590e3 commit 597bbef
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"node": true
},
"rules": {
"import/no-unassigned-import": "off",
"tsdoc/syntax": "off"
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/**
* @jest-environment ./test/setup/collaborative.jest.env.ts
*/
/** @jest-environment ./test/setup/collaborative.jest.env.ts */

// eslint-disable-next-line import/no-unassigned-import
import '../setup/globals.jest'

describe('selection adjustment', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/**
* @jest-environment ./test/setup/collaborative.jest.env.ts
*/
/** @jest-environment ./test/setup/collaborative.jest.env.ts */

// eslint-disable-next-line import/no-unassigned-import
import '../setup/globals.jest'
import {PortableTextBlock} from '../../src'
import type {PortableTextBlock} from '../../src'

const initialValue: PortableTextBlock[] | undefined = [
{
Expand Down
2 changes: 2 additions & 0 deletions packages/@sanity/portable-text-editor/test/setup/afterEnv.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
jest.setTimeout(20 * 1000)

export {}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import childProcess from 'child_process'
import NodeEnvironment from 'jest-environment-node'
import puppeteer, {ElementHandle, KeyInput} from 'puppeteer'
import ipc from 'node-ipc'
import {isEqual} from 'lodash'
import {EditorSelection, PortableTextBlock} from '../../src'
import ipc from 'node-ipc'
import puppeteer, {ElementHandle, KeyInput} from 'puppeteer'
import {FLUSH_PATCHES_DEBOUNCE_MS} from '../../src/constants'
import {normalizeSelection} from '../../src/utils/selection'
import type {EditorSelection, PortableTextBlock} from '../../src'

ipc.config.id = 'collaborative-jest-environment-ipc-client'
ipc.config.retry = 1500
Expand Down Expand Up @@ -33,7 +33,7 @@ const launchConfig = process.env.CI

let testId: string

function generateRandomInteger(min, max) {
function generateRandomInteger(min: number, max: number) {
return Math.floor(min + Math.random() * (max - min + 1))
}

Expand All @@ -42,11 +42,13 @@ export const delay = (time: number): Promise<void> => {
setTimeout(resolve, time)
})
}

export default class CollaborationEnvironment extends NodeEnvironment {
private _browserA: puppeteer.Browser
private _browserB: puppeteer.Browser
private _pageA: puppeteer.Page
private _pageB: puppeteer.Page
private _browserA?: puppeteer.Browser
private _browserB?: puppeteer.Browser
private _pageA?: puppeteer.Page
private _pageB?: puppeteer.Page

public async setup(): Promise<void> {
await super.setup()
this._browserA = await puppeteer.launch(launchConfig)
Expand All @@ -56,10 +58,10 @@ export default class CollaborationEnvironment extends NodeEnvironment {

// Hook up page console and npm debug in the PTE
if (DEBUG) {
await this._pageA.evaluateOnNewDocument((filter) => {
await this._pageA.evaluateOnNewDocument((filter: string) => {
window.localStorage.debug = filter
}, DEBUG)
await this._pageB.evaluateOnNewDocument((filter) => {
await this._pageB.evaluateOnNewDocument((filter: string) => {
window.localStorage.debug = filter
}, DEBUG)
this._pageA.on('console', (message) =>
Expand Down Expand Up @@ -99,22 +101,22 @@ export default class CollaborationEnvironment extends NodeEnvironment {

private async _setupInstance(): Promise<void> {
testId = (Math.random() + 1).toString(36).substring(7)
await this._pageA.goto(`${WEB_SERVER_ROOT_URL}?editorId=A&testId=${testId}`)
await this._pageB.goto(`${WEB_SERVER_ROOT_URL}?editorId=B&testId=${testId}`)
await this._pageA?.goto(`${WEB_SERVER_ROOT_URL}?editorId=A&testId=${testId}`)
await this._pageB?.goto(`${WEB_SERVER_ROOT_URL}?editorId=B&testId=${testId}`)
this.global.setDocumentValue = async (
value: PortableTextBlock[] | undefined
): Promise<void> => {
ipc.of.socketServer.emit('payload', JSON.stringify({type: 'value', value, testId}))
const [valueHandleA, valueHandleB] = await Promise.all([
this._pageA.waitForSelector('#pte-value'),
this._pageB.waitForSelector('#pte-value'),
this._pageA?.waitForSelector('#pte-value'),
this._pageB?.waitForSelector('#pte-value'),
])

if (!valueHandleA || !valueHandleB) {
throw new Error('Failed to find `#pte-value` element on page')
}

const readVal = (node) => {
const readVal = (node: any) => {
return node.innerText ? JSON.parse(node.innerText) : undefined
}
if (valueHandleA === null || valueHandleB === null) {
Expand All @@ -131,7 +133,7 @@ export default class CollaborationEnvironment extends NodeEnvironment {
}
this.global.getEditors = () =>
Promise.all(
[this._pageA, this._pageB].map(async (page, index) => {
[this._pageA!, this._pageB!].map(async (page, index) => {
const userAgent = await page.evaluate(() => navigator.userAgent)
const isMac = /Mac|iPod|iPhone|iPad/.test(userAgent)
const metaKey = isMac ? 'Meta' : 'Control'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const path = require('path')
const {setup: setupDevServer} = require('jest-dev-server')
import path from 'path'
import {setup as setupDevServer} from 'jest-dev-server'

const testFolderPath = path.resolve(__dirname, '..')

module.exports = async function globalSetup() {
export default async function globalSetup() {
await setupDevServer([
{
command: `vite --port 3000 ${testFolderPath}/web-server`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {teardown: teardownDevServer} = require('jest-dev-server')
import {teardown as teardownDevServer} from 'jest-dev-server'

module.exports = async function globalTeardown() {
export default async function globalTeardown() {
await teardownDevServer()
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {KeyInput} from 'puppeteer'
import {EditorSelection, PortableTextBlock} from '../../src'

export default {}
export {}

type Value = PortableTextBlock[] | undefined

Expand All @@ -18,7 +18,8 @@ type Editor = {
toggleMark: () => Promise<void>
undo: () => void
}

declare global {
function getEditors(): Promise<Editor[]>
function setDocumentValue(value: Value): void
function setDocumentValue(value: Value): Promise<void>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Box, Card, Stack, studioTheme, ThemeProvider} from '@sanity/ui'
import React, {useCallback, useMemo, useState} from 'react'

import ReactDOM from 'react-dom'
import {Subject} from 'rxjs'
import {EditorSelection, Patch, PortableTextBlock} from '../../src'
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/portable-text-editor/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "../../../tsconfig.settings",
"include": ["./src"],
"include": ["./src", "./test"],
"compilerOptions": {
"composite": true,
"rootDir": ".",
Expand Down

0 comments on commit 597bbef

Please sign in to comment.