From 43646b6b39da07690422431fb96747a8dba345c2 Mon Sep 17 00:00:00 2001 From: Amandeep Singh Date: Fri, 28 Aug 2020 10:54:42 +1000 Subject: [PATCH] Add tests for `OEmbed` field (#3462) --- .changeset/tender-eyes-joke.md | 6 + .github/workflows/tests.yml | 1 + api-tests/fields/crud.test.js | 30 ++++- packages/fields-oembed/.env.example | 2 + packages/fields-oembed/package.json | 2 + packages/fields-oembed/src/test-fixtures.js | 135 +++++++++++++++++++- 6 files changed, 166 insertions(+), 10 deletions(-) create mode 100644 .changeset/tender-eyes-joke.md create mode 100644 packages/fields-oembed/.env.example diff --git a/.changeset/tender-eyes-joke.md b/.changeset/tender-eyes-joke.md new file mode 100644 index 00000000000..1c6285c5c82 --- /dev/null +++ b/.changeset/tender-eyes-joke.md @@ -0,0 +1,6 @@ +--- +'@keystonejs/api-tests': patch +'@keystonejs/fields-oembed': patch +--- + +Added filter and CRUD tests for `OEmbed` field type. diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ffff1e420da..78c75dbca75 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -56,6 +56,7 @@ jobs: env: CI_NODE_TOTAL: 5 CI_NODE_INDEX: ${{ matrix.index }} + IFRAMELY_API_KEY: ${{ secrets.IFRAMELY_API_KEY }} linting: name: Linting diff --git a/api-tests/fields/crud.test.js b/api-tests/fields/crud.test.js index 3b493272605..3c658005a22 100644 --- a/api-tests/fields/crud.test.js +++ b/api-tests/fields/crud.test.js @@ -43,14 +43,20 @@ multiAdapterRunners().map(({ runner, adapterName }) => ); describe(`${mod.name} - CRUD operations`, () => { - const { fieldName, exampleValue, exampleValue2 } = mod; - const returnFields = `id name ${fieldName}`; + const { fieldName, exampleValue, exampleValue2, subfieldName } = mod; + + // Some field types can have subfields + const returnFields = subfieldName + ? `id name ${fieldName} { ${subfieldName} }` + : `id name ${fieldName}`; + const withHelpers = wrappedFn => { return async ({ keystone, listKey }) => { const items = await getItems({ keystone, listKey, returnFields, + sortBy: 'name_ASC', }); return wrappedFn({ keystone, listKey, items }); }; @@ -70,7 +76,9 @@ multiAdapterRunners().map(({ runner, adapterName }) => returnFields, }); expect(data).not.toBe(null); - expect(data[fieldName]).toBe(exampleValue); + expect(subfieldName ? data[fieldName][subfieldName] : data[fieldName]).toBe( + exampleValue + ); }) ) ); @@ -88,7 +96,9 @@ multiAdapterRunners().map(({ runner, adapterName }) => returnFields, }); expect(data).not.toBe(null); - expect(data[fieldName]).toBe(items[0][fieldName]); + expect(subfieldName ? data[fieldName][subfieldName] : data[fieldName]).toBe( + subfieldName ? items[0][fieldName][subfieldName] : items[0][fieldName] + ); }) ) ); @@ -110,7 +120,9 @@ multiAdapterRunners().map(({ runner, adapterName }) => returnFields, }); expect(data).not.toBe(null); - expect(data[fieldName]).toBe(exampleValue2); + expect(subfieldName ? data[fieldName][subfieldName] : data[fieldName]).toBe( + exampleValue2 + ); }) ) ); @@ -149,7 +161,9 @@ multiAdapterRunners().map(({ runner, adapterName }) => }); expect(data).not.toBe(null); expect(data.name).toBe('Updated value'); - expect(data[fieldName]).toBe(items[0][fieldName]); + expect(subfieldName ? data[fieldName][subfieldName] : data[fieldName]).toBe( + subfieldName ? items[0][fieldName][subfieldName] : items[0][fieldName] + ); }) ) ); @@ -169,7 +183,9 @@ multiAdapterRunners().map(({ runner, adapterName }) => }); expect(data).not.toBe(null); expect(data.name).toBe(items[0].name); - expect(data[fieldName]).toBe(items[0][fieldName]); + expect(subfieldName ? data[fieldName][subfieldName] : data[fieldName]).toBe( + subfieldName ? items[0][fieldName][subfieldName] : items[0][fieldName] + ); const allItems = await getItems({ keystone, diff --git a/packages/fields-oembed/.env.example b/packages/fields-oembed/.env.example new file mode 100644 index 00000000000..8ffde91ec34 --- /dev/null +++ b/packages/fields-oembed/.env.example @@ -0,0 +1,2 @@ +# Get a free account at https://iframely.com +# IFRAMELY_API_KEY=12121fasdfaf212121sdf diff --git a/packages/fields-oembed/package.json b/packages/fields-oembed/package.json index cc98ec54424..6bfa36651ce 100644 --- a/packages/fields-oembed/package.json +++ b/packages/fields-oembed/package.json @@ -29,6 +29,8 @@ "react": "^16.13.1" }, "devDependencies": { + "@keystonejs/server-side-graphql-client": "*", + "dotenv": "^8.2.0", "react": "^16.13.1" }, "main": "dist/fields-oembed.cjs.js", diff --git a/packages/fields-oembed/src/test-fixtures.js b/packages/fields-oembed/src/test-fixtures.js index 2258b61d073..b594a5223ae 100644 --- a/packages/fields-oembed/src/test-fixtures.js +++ b/packages/fields-oembed/src/test-fixtures.js @@ -1,7 +1,136 @@ -import { OEmbed } from './'; +const path = require('path'); +require('dotenv').config({ path: path.resolve(__dirname, '../.env') }); +import { getItems } from '@keystonejs/server-side-graphql-client'; + +import { OEmbed, IframelyOEmbedAdapter } from './'; export const name = 'OEmbed'; export { OEmbed as type }; +export const exampleValue = 'https://jestjs.io'; +export const exampleValue2 = 'https://codesandbox.io'; export const supportsUnique = false; -export const skipRequiredTest = true; -export const skipCrudTest = true; +export const fieldName = 'portfolio'; +export const subfieldName = 'originalUrl'; + +const iframelyAdapter = new IframelyOEmbedAdapter({ + apiKey: process.env.IFRAMELY_API_KEY, +}); + +export const fieldConfig = { adapter: iframelyAdapter }; + +export const getTestFields = () => { + return { + name: { type: String }, + portfolio: { type: OEmbed, adapter: iframelyAdapter }, + }; +}; + +export const initItems = () => { + return [ + { name: 'a', portfolio: 'https://github.com' }, + { name: 'b', portfolio: 'https://keystonejs.com' }, + { name: 'c', portfolio: 'https://reactjs.org' }, + { name: 'd', portfolio: 'https://REACTJS.ORG' }, + { name: 'e', portfolio: null }, + { name: 'f' }, + ]; +}; + +export const filterTests = withKeystone => { + const match = async (keystone, where, expected, sortBy = 'name_ASC') => + expect( + await getItems({ + keystone, + listKey: 'Test', + where, + returnFields: 'name portfolio { originalUrl }', + sortBy, + }) + ).toEqual(expected); + + test( + 'No filter', + withKeystone(({ keystone }) => + match(keystone, undefined, [ + { name: 'a', portfolio: { originalUrl: 'https://github.com' } }, + { + name: 'b', + portfolio: { originalUrl: 'https://keystonejs.com' }, + }, + { name: 'c', portfolio: { originalUrl: 'https://reactjs.org' } }, + { name: 'd', portfolio: { originalUrl: 'https://REACTJS.ORG' } }, + { name: 'e', portfolio: null }, + { name: 'f', portfolio: null }, + ]) + ) + ); + + test( + 'Empty filter', + withKeystone(({ keystone }) => + match(keystone, {}, [ + { name: 'a', portfolio: { originalUrl: 'https://github.com' } }, + { + name: 'b', + portfolio: { originalUrl: 'https://keystonejs.com' }, + }, + { name: 'c', portfolio: { originalUrl: 'https://reactjs.org' } }, + { name: 'd', portfolio: { originalUrl: 'https://REACTJS.ORG' } }, + { name: 'e', portfolio: null }, + { name: 'f', portfolio: null }, + ]) + ) + ); + + test( + 'Filter: portfolio_not null', + withKeystone(({ keystone }) => + match(keystone, { portfolio_not: null }, [ + { name: 'a', portfolio: { originalUrl: 'https://github.com' } }, + { + name: 'b', + portfolio: { originalUrl: 'https://keystonejs.com' }, + }, + { name: 'c', portfolio: { originalUrl: 'https://reactjs.org' } }, + { name: 'd', portfolio: { originalUrl: 'https://REACTJS.ORG' } }, + ]) + ) + ); + + test( + 'Filter: portfolio_not_in null', + withKeystone(({ keystone }) => + match(keystone, { portfolio_not_in: [null] }, [ + { name: 'a', portfolio: { originalUrl: 'https://github.com' } }, + { + name: 'b', + portfolio: { originalUrl: 'https://keystonejs.com' }, + }, + { name: 'c', portfolio: { originalUrl: 'https://reactjs.org' } }, + { name: 'd', portfolio: { originalUrl: 'https://REACTJS.ORG' } }, + ]) + ) + ); + + test( + 'Filter: portfolio_in (empty list)', + withKeystone(({ keystone }) => match(keystone, { portfolio_in: [] }, [])) + ); + + test( + 'Filter: portfolio_not_in (empty list)', + withKeystone(({ keystone }) => + match(keystone, { portfolio_not_in: [] }, [ + { name: 'a', portfolio: { originalUrl: 'https://github.com' } }, + { + name: 'b', + portfolio: { originalUrl: 'https://keystonejs.com' }, + }, + { name: 'c', portfolio: { originalUrl: 'https://reactjs.org' } }, + { name: 'd', portfolio: { originalUrl: 'https://REACTJS.ORG' } }, + { name: 'e', portfolio: null }, + { name: 'f', portfolio: null }, + ]) + ) + ); +};