Skip to content

Commit

Permalink
Merge pull request #31 from contentful/feat/tol-1000/primitive-gql-fi…
Browse files Browse the repository at this point in the history
…elds

feat: live update primitive field values
  • Loading branch information
YvesRijckaert authored Mar 22, 2023
2 parents 78496c6 + 5d76695 commit f8c5d74
Show file tree
Hide file tree
Showing 10 changed files with 808 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"homepage": "https://github.com/contentful/live-preview#readme",
"dependencies": {
"contentful-management": "^10.30.1",
"use-deep-compare-effect": "^1.8.1"
},
"devDependencies": {
Expand Down
94 changes: 94 additions & 0 deletions src/graphql/__tests__/entries.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { EntryProps } from 'contentful-management/types';
import { describe, it, expect, vi, afterEach } from 'vitest';

import { updateEntry } from '../entries';
import contentType from './fixtures/contentType.json';
import entry from './fixtures/entry.json';

const EN = 'en-US';
// const DE = 'de';

describe('Update GraphQL Entry', () => {
afterEach(() => {
vi.clearAllMocks();
});

const updateFn = ({
data,
update = entry as EntryProps,
locale = EN,
}: {
data: Record<string, unknown>;
update?: EntryProps;
locale?: string;
}) => {
return updateEntry(contentType, data, update, locale);
};

it('keeps __typename unchanged', () => {
const warn = vi.spyOn(console, 'warn');
const data = { __typename: 'CT', shortText: 'text' };

const update = updateFn({ data });

expect(update).toEqual(
expect.objectContaining({
__typename: 'CT',
})
);
expect(warn).not.toHaveBeenCalled();
});

it('warns but keeps unknown fields', () => {
const data = { unknownField: 'text' };
const warn = vi.spyOn(console, 'warn');

const update = updateFn({ data });

expect(update).toEqual(data);
expect(warn).toHaveBeenCalledWith(expect.stringMatching(/Unrecognized field 'unknownField'/));
});

it('updates primitive fields', () => {
const data = {
shortText: 'oldValue',
shortTextList: ['oldValue'],
longText: 'oldValue',
boolean: false,
numberInteger: -1,
numberDecimal: -1.0,
dateTime: '1970-1-1T00:00+00:00',
location: {
lon: 0,
lat: 0,
},
json: {
test: 'oldValue',
},
};

expect(updateFn({ data })).toEqual({
shortText: entry.fields.shortText[EN],
shortTextList: entry.fields.shortTextList[EN],
longText: entry.fields.longText[EN],
boolean: entry.fields.boolean[EN],
numberInteger: entry.fields.numberInteger[EN],
numberDecimal: entry.fields.numberDecimal[EN],
dateTime: entry.fields.dateTime[EN],
location: entry.fields.location[EN],
json: entry.fields.json[EN],
});
});

it('falls back to null for empty fields', () => {
const data = {
shortText: 'oldValue',
};

const update = updateFn({ data, locale: 'n/a' });

expect(update).toEqual({
shortText: null,
});
});
});
289 changes: 289 additions & 0 deletions src/graphql/__tests__/fixtures/contentType.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "80q6jb175og9"
}
},
"id": "fieldTypes",
"type": "ContentType",
"createdAt": "2023-03-14T18:36:32.224Z",
"updatedAt": "2023-03-15T07:45:51.251Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"publishedVersion": 7,
"publishedAt": "2023-03-15T07:45:51.251Z",
"firstPublishedAt": "2023-03-14T18:36:32.695Z",
"createdBy": {
"sys": {
"type": "Link",
"linkType": "User",
"id": "4LSGWE90WTQF3IXCiTEP74"
}
},
"updatedBy": {
"sys": {
"type": "Link",
"linkType": "User",
"id": "4LSGWE90WTQF3IXCiTEP74"
}
},
"publishedCounter": 4,
"version": 8,
"publishedBy": {
"sys": {
"type": "Link",
"linkType": "User",
"id": "4LSGWE90WTQF3IXCiTEP74"
}
}
},
"displayField": "vDOpnJnlacj5Vd6p",
"name": "FieldTypes",
"description": "",
"assembly": false,
"fields": [
{
"id": "vDOpnJnlacj5Vd6p",
"apiName": "shortText",
"name": "Short Text",
"type": "Symbol",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false
},
{
"id": "phV9Yneb9X0ZLny2",
"apiName": "shortTextList",
"name": "Short Text (List)",
"type": "Array",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false,
"items": {
"type": "Symbol",
"validations": []
}
},
{
"id": "j3HFWaem7j8YYOX1",
"apiName": "longText",
"name": "Long Text",
"type": "Text",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false
},
{
"id": "vBGzBcEXwwxIE2hy",
"apiName": "numberInteger",
"name": "Number (Integer)",
"type": "Integer",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false
},
{
"id": "K9qMpAog8ipH2j3h",
"apiName": "numberDecimal",
"name": "Number (Decimal)",
"type": "Integer",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false
},
{
"id": "nbny24EE0Q2JYvAg",
"apiName": "dateTime",
"name": "Date & Time",
"type": "Date",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false
},
{
"id": "JPjeGdLfiAAEXxAo",
"apiName": "location",
"name": "Location",
"type": "Location",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false
},
{
"id": "HV7C9mvymQaHVGex",
"apiName": "json",
"name": "JSON",
"type": "Object",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false
},
{
"id": "vY6Yi341M9Ve2jhv",
"apiName": "media",
"name": "Media",
"type": "Link",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false,
"linkType": "Asset"
},
{
"id": "n5PCVXCdecdQijDe",
"apiName": "mediaMany",
"name": "Media (Many)",
"type": "Array",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false,
"items": {
"type": "Link",
"validations": [],
"linkType": "Asset"
}
},
{
"id": "mwfJkx0Y8L1hNkA3",
"apiName": "boolean",
"name": "Boolean",
"type": "Boolean",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false
},
{
"id": "Z9HGbIbdXDMzcCJz",
"apiName": "richText",
"name": "Rich Text",
"type": "RichText",
"localized": false,
"required": false,
"validations": [
{
"enabledNodeTypes": [
"heading-1",
"heading-2",
"heading-3",
"heading-4",
"heading-5",
"heading-6",
"ordered-list",
"unordered-list",
"hr",
"blockquote",
"embedded-entry-block",
"embedded-asset-block",
"table",
"hyperlink",
"entry-hyperlink",
"asset-hyperlink",
"embedded-entry-inline"
]
},
{
"enabledMarks": ["bold", "italic", "underline", "code", "superscript", "subscript"]
}
],
"disabled": false,
"omitted": false
},
{
"id": "cLWeBMwi5iaPXcI1",
"apiName": "reference",
"name": "Reference",
"type": "Link",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false,
"linkType": "Entry"
},
{
"id": "Aoz98jYkj8lMnyFi",
"apiName": "referenceMany",
"name": "Reference (Many)",
"type": "Array",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false,
"items": {
"type": "Link",
"validations": [],
"linkType": "Entry"
}
},
{
"id": "PIh43g0NQmzDYo1j",
"apiName": "crossSpaceReference",
"name": "Cross Space Reference",
"type": "ResourceLink",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false,
"allowedResources": [
{
"type": "Contentful:Entry",
"source": "crn:contentful:::content:spaces/3yzzz65gh9tw",
"contentTypes": ["crossSpaceContentType"]
}
]
},
{
"id": "CCJZmPQ1oYq45nQi",
"apiName": "crossSpaceReferenceMany",
"name": "Cross Space Reference (Many)",
"type": "Array",
"localized": false,
"required": false,
"validations": [],
"disabled": false,
"omitted": false,
"items": {
"type": "ResourceLink",
"validations": []
},
"allowedResources": [
{
"type": "Contentful:Entry",
"source": "crn:contentful:::content:spaces/3yzzz65gh9tw",
"contentTypes": ["crossSpaceContentType"]
}
]
}
]
}
Loading

0 comments on commit f8c5d74

Please sign in to comment.