Skip to content

Commit

Permalink
test: fix missing await on assertions for resolveInitialValue() (#8080
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rexxars authored Dec 17, 2024
1 parent e5cf80a commit f56c61f
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions packages/sanity/src/core/templates/__tests__/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,50 @@ const mockConfigContext: InitialValueResolverContext = {
} as InitialValueResolverContext

describe('resolveInitialValue', () => {
test('serializes builders', () => {
expect(resolveInitialValue(schema, example, {}, mockConfigContext)).resolves.toMatchObject({
test('serializes builders', async () => {
await expect(
resolveInitialValue(schema, example, {}, mockConfigContext),
).resolves.toMatchObject({
title: 'here',
})
})

test('works with raw templates', () => {
expect(resolveInitialValue(schema, example, {}, mockConfigContext)).resolves.toMatchObject({
test('works with raw templates', async () => {
await expect(
resolveInitialValue(schema, example, {}, mockConfigContext),
).resolves.toMatchObject({
title: 'here',
})
})

test('throws on missing template `value` prop', () => {
expect(
test('throws on missing template `value` prop', async () => {
await expect(
resolveInitialValue(schema, omit(example, ['value']) as Template, {}, mockConfigContext),
).rejects.toMatchObject({
message: 'Template "author" has invalid "value" property',
})
})

test('throws on non-function/non-object template `value` prop', () => {
expect(
test('throws on non-function/non-object template `value` prop', async () => {
await expect(
resolveInitialValue(schema, {...example, value: []}, {}, mockConfigContext),
).rejects.toMatchObject({
message:
'Template "author" has invalid "value" property - must be a plain object or a resolver function returning a plain object',
})
})

test('throws on wrong `_type` prop', () => {
expect(
test('throws on wrong `_type` prop', async () => {
await expect(
resolveInitialValue(schema, {...example, value: {_type: 'foo'}}, {}, mockConfigContext),
).rejects.toMatchObject({
message:
'Template "author" initial value: includes "_type"-property (foo) that does not match template (author)',
})
})

test('throws on unknown prop in reference', () => {
expect(
test('throws on unknown prop in reference', async () => {
await expect(
resolveInitialValue(
schema,
{...example, value: {bestFriend: {_ref: 'grrm', name: 'GRRM'}}},
Expand All @@ -82,8 +86,8 @@ describe('resolveInitialValue', () => {
})
})

test('throws on unknown props in reference', () => {
expect(
test('throws on unknown props in reference', async () => {
await expect(
resolveInitialValue(
schema,
{...example, value: {bestFriend: {_ref: 'grrm', name: 'GRRM', age: 72}}},
Expand All @@ -96,8 +100,8 @@ describe('resolveInitialValue', () => {
})
})

test('allows setting known reference properties', () => {
expect(
test('allows setting known reference properties', async () => {
await expect(
resolveInitialValue(
schema,
{
Expand All @@ -124,8 +128,8 @@ describe('resolveInitialValue', () => {
})
})

test('allows setting _dataset on cross-dataset references', () => {
expect(
test('allows setting _dataset on cross-dataset references', async () => {
await expect(
resolveInitialValue(
schema,
{
Expand All @@ -152,16 +156,16 @@ describe('resolveInitialValue', () => {
})
})

test('should call sync value resolvers', () => {
expect(
test('should call sync value resolvers', async () => {
await expect(
resolveInitialValue(schema, {...example, value: () => example.value}, {}, mockConfigContext),
).resolves.toMatchObject({
title: 'here',
})
})

test('should call async value resolvers', () => {
expect(
test('should call async value resolvers', async () => {
await expect(
resolveInitialValue(
schema,
{
Expand All @@ -176,8 +180,8 @@ describe('resolveInitialValue', () => {
})
})

test('throws on wrong value type resolved', () => {
expect(
test('throws on wrong value type resolved', async () => {
await expect(
resolveInitialValue(schema, {...example, value: () => null}, {}, mockConfigContext),
).rejects.toMatchObject({
message:
Expand All @@ -188,8 +192,8 @@ describe('resolveInitialValue', () => {
// todo: we should validate based on schema type here and reenable this test
// Currently the initial value validator is not schema aware and fails if resolved initial value is missing _type
// but this doesn't account for fields of type object, which is a valid case for omitting _type.
test.skip('throws on values with sub-objects missing `_type`', () => {
expect(
test.skip('throws on values with sub-objects missing `_type`', async () => {
await expect(
resolveInitialValue(
schema,
{
Expand All @@ -204,8 +208,8 @@ describe('resolveInitialValue', () => {
})
})

test('applies missing `_type` to references', () => {
expect(
test('applies missing `_type` to references', async () => {
await expect(
resolveInitialValue(
schema,
{
Expand Down

0 comments on commit f56c61f

Please # to comment.