Skip to content

Commit

Permalink
test(comments): omit inline comments without referenced value
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanwikner committed Feb 15, 2024
1 parent 61b2dbc commit 1f9a592
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Schema} from '@sanity/schema'
import {type CurrentUser, defineField} from '@sanity/types'

import {buildCommentBreadcrumbs} from '../utils'
import {buildTestComment} from './helpers'

const CURRENT_USER: CurrentUser = {
email: '',
Expand Down Expand Up @@ -42,6 +43,13 @@ const arrayOfObjectsField = defineField({
of: [objectField],
})

const myBlockField = defineField({
name: 'myBlock',
title: 'My block',
type: 'array',
of: [{type: 'block'}],
})

const nestedArrayOfObjectsField = defineField({
name: 'myNestedArray',
title: 'My nested array title',
Expand Down Expand Up @@ -108,6 +116,7 @@ const schema = Schema.compile({
nestedArrayOfObjectsField,
arrayWithAnonymousObjectField,
arrayWithAnonymousObjectFieldWithHiddenCallback,
myBlockField,
],
},
],
Expand Down Expand Up @@ -374,11 +383,104 @@ describe('comments: buildCommentBreadcrumbs', () => {
expect(crumbs).toEqual([
{
invalid: false,

title: 'My array with anonymous object field with hidden callback title',
},
{invalid: false, isArrayItem: true, title: '#2'},
{invalid: true, title: 'Anonymous string with hidden callback title'},
])
})

it('should invalidate breadcrumbs if the inline comment do not have any referenced text in the value', () => {
const crumbs = buildCommentBreadcrumbs({
schemaType: schema.get('testDocument'),
fieldPath: 'myBlock',
currentUser: CURRENT_USER,
comment: buildTestComment({
path: {
field: 'myBlock',
selection: {
type: 'text',
value: [
{
_key: 'BLOCK_KEY',
text: 'Hello <comment>there</comment>',
},
],
},
},
}),
documentValue: {
myBlock: [
{
_key: 'BLOCK_KEY',
children: [
{
_type: 'span',
marks: [],
text: `Some text that the comment won't match with!`,
_key: 'TEXT_KEY',
},
],
markDefs: [],
_type: 'block',
style: 'normal',
},
],
},
})

expect(crumbs).toEqual([
{
invalid: true,
title: '',
},
])
})

it('should build breadcrumbs if the inline comment have a referenced text in the value', () => {
const crumbs = buildCommentBreadcrumbs({
schemaType: schema.get('testDocument'),
fieldPath: 'myBlock',
currentUser: CURRENT_USER,
comment: buildTestComment({
path: {
field: 'myBlock',
selection: {
type: 'text',
value: [
{
_key: 'BLOCK_KEY',
text: 'Some text that the comment <comment>will match</comment> with!',
},
],
},
},
}),
documentValue: {
myBlock: [
{
_key: 'BLOCK_KEY',
children: [
{
_type: 'span',
marks: [],
text: `Some text that the comment will match with!`,
_key: 'TEXT_KEY',
},
],
markDefs: [],
_type: 'block',
style: 'normal',
},
],
},
})

expect(crumbs).toEqual([
{
invalid: false,
title: 'My block',
},
])
})
})
32 changes: 32 additions & 0 deletions packages/sanity/src/structure/comments/src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {type CommentDocument, type CommentPath} from '../types'

interface BuildTestCommentProps {
path: CommentPath
}

export function buildTestComment(props: BuildTestCommentProps): CommentDocument {
const {path} = props

return {
_createdAt: '',
_id: '',
_rev: '',
_type: 'comment',
authorId: '',
message: [],
reactions: [],
status: 'open',
threadId: '',
target: {
document: {
_dataset: '',
_projectId: '',
_ref: '',
_type: 'crossDatasetReference',
_weak: true,
},
documentType: '',
path,
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ export function buildCommentBreadcrumbs(
const parentValue = getValueAtPath(documentValue, previousPath)
const currentValue = getValueAtPath(documentValue, currentPath)

if (comment && isTextSelectionComment(comment)) {
const conditionalContext: ConditionalPropertyCallbackContext = {
document: documentValue as SanityDocument,
currentUser,
parent: parentValue,
value: currentValue,
}

if (comment && isTextSelectionComment(comment) && field) {
const selection = buildRangeDecorationSelectionsFromComment({
comment,
value: (currentValue || []) as PortableTextBlock[],
Expand All @@ -101,19 +108,23 @@ export function buildCommentBreadcrumbs(
// and will be omitted from the comments array in `buildCommentThreadItems`.
const noSelections = selection.length === 0

if (noSelections) {
const hidden = resolveConditionalProperty(field.type.hidden, conditionalContext)

if (noSelections || hidden) {
fieldPaths.push({
invalid: true,
title: 'No referenced text found for comment',
title: '',
})
} else {
fieldPaths.push({
title: getSchemaTypeTitle(field.type),
invalid: false,
})
}
}

const conditionalContext: ConditionalPropertyCallbackContext = {
document: documentValue as SanityDocument,
currentUser,
parent: parentValue,
value: currentValue,
currentSchemaType = field.type

return
}

// If the field is a key segment and the parent value is an array, we'll
Expand Down

0 comments on commit 1f9a592

Please sign in to comment.