-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(gql): inifite loop with circular references [] #383
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
121 changes: 28 additions & 93 deletions
121
packages/live-preview-sdk/src/__tests__/resolveReference.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,61 @@ | ||
/* @vitest-environment jsdom */ | ||
import { EditorEntityStore } from '@contentful/visual-sdk'; | ||
import type { Asset, Entry } from 'contentful'; | ||
import { describe, vi, it, beforeEach, afterEach, expect, Mock } from 'vitest'; | ||
|
||
import { resolveReference } from '../helpers/resolveReference'; | ||
import cdaAsset from './fixtures/cdaAsset.json'; | ||
import cdaEntry from './fixtures/cdaEntry.json'; | ||
import type { GetStore } from '../types'; | ||
|
||
vi.mock('@contentful/visual-sdk'); | ||
|
||
describe('resolveReference', () => { | ||
const locale = 'en-US'; | ||
const sendMessage = vi.fn(); | ||
const getStore = vi.fn<Parameters<GetStore>, ReturnType<GetStore>>(); | ||
let store: EditorEntityStore; | ||
|
||
beforeEach(() => { | ||
(EditorEntityStore as Mock).mockImplementation(() => ({ | ||
__mocked__: true, | ||
fetchEntry: async () => cdaEntry, | ||
fetchAsset: async () => cdaAsset, | ||
})); | ||
|
||
store = new EditorEntityStore({ | ||
entities: [], | ||
locale, | ||
sendMessage: vi.fn(), | ||
subscribe: vi.fn(), | ||
timeoutDuration: 10, | ||
}); | ||
|
||
getStore.mockImplementation(() => store); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe('asset', () => { | ||
const asset = { | ||
fields: { | ||
file: { fileName: 'abc.jpg' }, | ||
}, | ||
sys: { | ||
id: '1', | ||
}, | ||
} as unknown as Asset; | ||
|
||
const expected = { | ||
reference: { | ||
fields: { | ||
file: { fileName: 'abc.jpg' }, | ||
}, | ||
sys: { | ||
id: '1', | ||
}, | ||
}, | ||
typeName: 'Asset', | ||
}; | ||
|
||
it('resolves it directly from the map', async () => { | ||
const result = await resolveReference({ | ||
entityReferenceMap: new Map([['1', asset]]), | ||
referenceId: '1', | ||
locale, | ||
sendMessage, | ||
}); | ||
|
||
expect(result).toEqual(expected); | ||
it('resolves an assets correctly from the store', async () => { | ||
const result = await resolveReference({ | ||
referenceId: cdaAsset.sys.id, | ||
isAsset: true, | ||
locale, | ||
getStore, | ||
}); | ||
|
||
it('resolves it from the editor', async () => { | ||
const result = await resolveReference({ | ||
entityReferenceMap: new Map(), | ||
referenceId: cdaAsset.sys.id, | ||
isAsset: true, | ||
locale, | ||
sendMessage, | ||
}); | ||
|
||
expect(result.typeName).toBe('Asset'); | ||
expect(result.reference.fields).toEqual(cdaAsset.fields); | ||
}); | ||
expect(result.typeName).toBe('Asset'); | ||
expect(result.reference.fields).toEqual(cdaAsset.fields); | ||
}); | ||
|
||
describe('entries', () => { | ||
const entry = { | ||
fields: { | ||
title: 'Hello World', | ||
}, | ||
sys: { | ||
id: '1', | ||
contentType: { | ||
sys: { id: 'helloWorld' }, | ||
}, | ||
}, | ||
} as unknown as Entry; | ||
|
||
const expected = { | ||
reference: { | ||
fields: { | ||
title: 'Hello World', | ||
}, | ||
sys: { | ||
id: '1', | ||
contentType: { | ||
sys: { id: 'helloWorld' }, | ||
}, | ||
}, | ||
}, | ||
typeName: 'HelloWorld', | ||
}; | ||
|
||
it('resolves it directly from the map', async () => { | ||
const result = await resolveReference({ | ||
entityReferenceMap: new Map([['1', entry]]), | ||
referenceId: '1', | ||
locale, | ||
sendMessage, | ||
}); | ||
|
||
expect(result).toEqual(expected); | ||
it('resolves an entry correctly from the store', async () => { | ||
const result = await resolveReference({ | ||
referenceId: cdaEntry.sys.id, | ||
locale, | ||
getStore, | ||
}); | ||
|
||
it('resolves it from the editor', async () => { | ||
const result = await resolveReference({ | ||
entityReferenceMap: new Map(), | ||
referenceId: cdaEntry.sys.id, | ||
locale, | ||
sendMessage, | ||
}); | ||
|
||
expect(result.typeName).toBe('TopicProductFeature'); | ||
expect(result.reference.fields).toEqual(cdaEntry.fields); | ||
}); | ||
expect(result.typeName).toBe('TopicProductFeature'); | ||
expect(result.reference.fields).toEqual(cdaEntry.fields); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why we need 2 for rich text?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 Level depth for the direct references
Assuming it's an entry it could be relevant to resolve also the asset of this entry, therefore then 2