Skip to content

Commit

Permalink
test(desk): fix resolve intent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Aug 16, 2022
1 parent a1fc4ca commit af975dd
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 48 deletions.
8 changes: 6 additions & 2 deletions packages/sanity/src/config/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {first, map} from 'rxjs/operators'
import {CurrentUser} from '@sanity/types'
import {SanityClient} from '@sanity/client'
import {createMockAuthStore} from '../datastores/authStore/createMockAuthStore'
import {Config, SingleWorkspace, Source, Workspace, WorkspaceOptions} from './types'
import {Config, SingleWorkspace, Source, Workspace, SchemaPluginOptions} from './types'
import {prepareConfig} from './prepareConfig'

/**
Expand Down Expand Up @@ -35,7 +35,11 @@ export function resolveConfig(config: Config): Observable<Workspace[]> {

type CreateWorkspaceFromConfigOptions =
| SingleWorkspace
| (SingleWorkspace & {currentUser: CurrentUser; client: SanityClient})
| (SingleWorkspace & {
currentUser: CurrentUser
client: SanityClient
schema?: SchemaPluginOptions
})

/**
* PRIMARILY FOR TESTING PURPOSES.
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export interface ConfigContext {
export type TemplateResolver = ComposableOption<Template[], ConfigContext>

export interface SchemaPluginOptions {
name?: string
types?:
| SchemaTypeDefinition[]
| ComposableOption<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {PaneNode, UnresolvedPaneNode} from '../../types'
import {createSchema} from '../../../schema'
import {SchemaPluginOptions} from '../../../config'
import {createStructureBuilder, SerializeError} from '../../structureBuilder'
import {resolveIntent} from '../resolveIntent'
import {PaneResolutionError} from '../PaneResolutionError'
import {getMockSource} from '../../../../test/testUtils/getMockWorkspaceFromConfig'

const mockSchema = createSchema({
const mockSchema: SchemaPluginOptions = {
name: 'mockSchema',
types: [
{
Expand Down Expand Up @@ -32,14 +33,12 @@ const mockSchema = createSchema({
fields: [{name: 'toggle', type: 'boolean'}],
},
],
})
}

describe('resolveIntent', () => {
it('takes in an intent request and returns `RouterPanes` that match the request', async () => {
const S = createStructureBuilder({
initialValueTemplates: [],
schema: mockSchema,
} as any)
const source = await getMockSource({schema: mockSchema})
const S = createStructureBuilder({source})

const rootPaneNode = S.list()
.title('Content')
Expand All @@ -65,10 +64,8 @@ describe('resolveIntent', () => {
})

it('resolves singletons', async () => {
const S = createStructureBuilder({
initialValueTemplates: [],
schema: mockSchema,
} as any)
const source = await getMockSource({schema: mockSchema})
const S = createStructureBuilder({source})

const rootPaneNode = S.list()
.title('Content')
Expand All @@ -94,10 +91,8 @@ describe('resolveIntent', () => {
})

it('resolves nested singletons', async () => {
const S = createStructureBuilder({
initialValueTemplates: [],
schema: mockSchema,
} as any)
const source = await getMockSource({schema: mockSchema})
const S = createStructureBuilder({source})

const rootPaneNode = S.list()
.title('Content')
Expand Down Expand Up @@ -143,10 +138,8 @@ describe('resolveIntent', () => {
})

it('returns the shallowest match', async () => {
const S = createStructureBuilder({
initialValueTemplates: [],
schema: mockSchema,
} as any)
const source = await getMockSource({schema: mockSchema})
const S = createStructureBuilder({source})

const rootPaneNode = S.list()
.title('Content')
Expand Down Expand Up @@ -200,10 +193,8 @@ describe('resolveIntent', () => {
})

it('resolves to the fallback editor if no match is found', async () => {
const S = createStructureBuilder({
initialValueTemplates: [],
schema: mockSchema,
} as any)
const source = await getMockSource({schema: mockSchema})
const S = createStructureBuilder({source})

const rootPaneNode = S.list()
.title('Content')
Expand All @@ -226,10 +217,8 @@ describe('resolveIntent', () => {
})

it('matches document nodes that have the same ID as the target ID', async () => {
const S = createStructureBuilder({
initialValueTemplates: [],
schema: mockSchema,
} as any)
const source = await getMockSource({schema: mockSchema})
const S = createStructureBuilder({source})

const rootPaneNode = S.list()
.title('Content')
Expand Down Expand Up @@ -258,10 +247,8 @@ describe('resolveIntent', () => {
})

it('resolves pane nodes that implement `canHandleIntent`', async () => {
const S = createStructureBuilder({
initialValueTemplates: [],
schema: mockSchema,
} as any)
const source = await getMockSource({schema: mockSchema})
const S = createStructureBuilder({source})

const list = S.list()
.canHandleIntent(() => true)
Expand Down Expand Up @@ -302,10 +289,8 @@ describe('resolveIntent', () => {
})

it('bubbles (re-throws) structure errors wrapped in a PaneResolutionError', async () => {
const S = createStructureBuilder({
initialValueTemplates: [],
schema: mockSchema,
} as any)
const source = await getMockSource({schema: mockSchema})
const S = createStructureBuilder({source})

const rootPaneNode = S.list().title('Content').items([
// will give a missing ID error
Expand Down
16 changes: 5 additions & 11 deletions packages/sanity/test/testUtils/TestProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import React from 'react'
import {SanityClient} from '@sanity/client'
import {LayerProvider, studioTheme, ThemeProvider, ToastProvider} from '@sanity/ui'
import {createWorkspaceFromConfig, SingleWorkspace} from '../../src/config'
import {SingleWorkspace} from '../../src/config'
import {SourceProvider, WorkspaceProvider} from '../../src/studio'
import {ResourceCacheProvider} from '../../src/datastores/ResourceCacheProvider'
import {getMockWorkspace} from './getMockWorkspaceFromConfig'

interface TestProviderOptions {
config: SingleWorkspace
client: SanityClient
config?: SingleWorkspace
client?: SanityClient
}

export async function createTestProvider({client, config}: TestProviderOptions) {
const currentUser = {
id: 'doug',
name: 'Doug',
email: 'doug@sanity.io',
role: 'admin',
roles: [{name: 'admin', title: 'Admin'}],
}
const workspace = await createWorkspaceFromConfig({...config, currentUser, client})
const workspace = await getMockWorkspace({client, config})

function TestProvider({children}: {children: React.ReactNode}) {
return (
Expand Down
62 changes: 62 additions & 0 deletions packages/sanity/test/testUtils/getMockWorkspaceFromConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type {SanityClient} from '@sanity/client'
import type {CurrentUser} from '@sanity/types'
import {
createWorkspaceFromConfig,
SchemaPluginOptions,
SingleWorkspace,
Source,
Workspace,
} from '../../src/config'
import {createMockSanityClient} from '../mocks/mockSanityClient'

const defaultMockUser: CurrentUser = {
id: 'doug',
name: 'Doug',
email: 'doug@sanity.io',
role: 'admin',
roles: [{name: 'admin', title: 'Admin'}],
}

const defaultMockConfig: SingleWorkspace = {
projectId: 'mock-project-id',
dataset: 'mock-data-set',
}

const defaultMockSchema: SchemaPluginOptions = {
name: 'mock',
types: [
{
name: 'author',
title: 'Author',
type: 'document',
fields: [
{
name: 'name',
title: 'Name',
type: 'string',
},
],
},
],
}

export interface MockWorkspaceOptions {
config?: SingleWorkspace
client?: SanityClient
schema?: SchemaPluginOptions
currentUser?: CurrentUser
}

export function getMockWorkspace({
config = defaultMockConfig,
currentUser = defaultMockUser,
client = createMockSanityClient() as any as SanityClient,
schema = defaultMockSchema,
}: MockWorkspaceOptions = {}): Promise<Workspace> {
return createWorkspaceFromConfig({...config, currentUser, client, schema})
}

export async function getMockSource(options: MockWorkspaceOptions = {}): Promise<Source> {
const workspace = await getMockWorkspace(options)
return workspace.unstable_sources[0]
}

0 comments on commit af975dd

Please sign in to comment.