Skip to content

Commit

Permalink
Add tests for OEmbed field (#3462)
Browse files Browse the repository at this point in the history
  • Loading branch information
singhArmani authored Aug 28, 2020
1 parent 07e246d commit 43646b6
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/tender-eyes-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystonejs/api-tests': patch
'@keystonejs/fields-oembed': patch
---

Added filter and CRUD tests for `OEmbed` field type.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 23 additions & 7 deletions api-tests/fields/crud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
};
Expand All @@ -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
);
})
)
);
Expand All @@ -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]
);
})
)
);
Expand All @@ -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
);
})
)
);
Expand Down Expand Up @@ -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]
);
})
)
);
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions packages/fields-oembed/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Get a free account at https://iframely.com
# IFRAMELY_API_KEY=12121fasdfaf212121sdf
2 changes: 2 additions & 0 deletions packages/fields-oembed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
135 changes: 132 additions & 3 deletions packages/fields-oembed/src/test-fixtures.js
Original file line number Diff line number Diff line change
@@ -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 },
])
)
);
};

0 comments on commit 43646b6

Please sign in to comment.