UNPKG

@financial-times/n-concept-ids

Version:
57 lines (47 loc) 1.62 kB
const ids = require('../src'); const fetch = require('isomorphic-fetch'); const fs = require('fs'); const {THINGS_API_KEY, FT_DEVELOPER_API_URL} = process.env; describe.each( Object.keys(ids).map(type => [type, Object.keys(ids[type]).map(name => [name, ids[type][name]])]) )('Type: %s', (type, members) => { if (type === 'collections') { expect(members[0][0]).toEqual('podcasts'); expect(members[0][1]).toContain('e609bfe0-9deb-4deb-ac65-135da462ffd8'); return; // avoid going into rest of loop } describe.each(members)('ID: %s', (name, id) => { let response; let json; beforeAll(async () => { response = await fetch(`${FT_DEVELOPER_API_URL}/things/${id}`, { headers: { 'x-api-key': THINGS_API_KEY } }); json = await response.json(); }); it('exists in Things API', () => { expect(response.status).toBe(200); }); it('has not been given a new ID', () => { expect(json.id.substr(json.id.lastIndexOf('/') + 1 )).toBe(id); }); it(`has the direct type: ${type}`, () => { expect(json.directType.substr(json.directType.lastIndexOf('/') + 1 ).toLowerCase()).toBe(type); }); }); }); describe('Data consistency', () => { // read the file to check for duplicate IDs with the same key name const sourceCode = fs.readFileSync(`${__dirname}/../src/index.js`, { encoding: 'utf8' }); const ids = sourceCode.match(/[a-z0-9-]{36}/g); const checkedIds = []; ids.forEach(id => { if (checkedIds.includes(id)) return; checkedIds.push(id); test(`ID ${id} is not a duplicate`, () => { expect(ids.filter(givenId => givenId === id).length).toBe(1); }); }); });