From ef065423ccb4d811f080d56ccc6a7228b5942cbd Mon Sep 17 00:00:00 2001 From: Sam Carlton Date: Sat, 21 May 2022 11:25:18 -0500 Subject: [PATCH] Add tests for api client --- package.json | 5 +-- test/api/client.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 test/api/client.js diff --git a/package.json b/package.json index 73bfec1..e12bc6e 100644 --- a/package.json +++ b/package.json @@ -20,9 +20,10 @@ }, "scripts": { "test-prebuild": "ava ./test/prebuild/**/*.js --verbose", - "test-prebuild-functions": "npm run test-prebuild && npm run test-listings", - "test-postbuild-api": "npm run test-listings", + "test-api-client": "ava ./test/api/client.js --verbose", "test-listings": "ava ./test/listings/**/*.js --verbose", + "test-prebuild-functions": "run-s test-prebuild test-api-client test-listings", + "test-postbuild-api": "run-s test-listings", "test": "ava --timeout=1m --verbose", "dev": "nuxt", "build": "nuxt build", diff --git a/test/api/client.js b/test/api/client.js new file mode 100644 index 0000000..90dc08d --- /dev/null +++ b/test/api/client.js @@ -0,0 +1,79 @@ +import test from 'ava' + +import { + generateAPI +} from '~/helpers/api/client.js' + + +const listingsCases = [ + + // Spotify + [ + '/app/spotify', + { + generateOptions: {}, + method: DoesItAPI => DoesItAPI.app.spotify.get(), + expected: { + name: 'Spotify', + } + } + ], + + // Electron + [ + '/app/electron-framework', + { + generateOptions: {}, + method: DoesItAPI => DoesItAPI.app('electron-framework').get(), + expected: { + name: 'Electron Framework', + } + } + ], + + // Express VPN Benchmarks + [ + '/app/expressvpn/benchmarks/', + { + generateOptions: {}, + method: DoesItAPI => DoesItAPI.app.expressvpn.get(), + expected: { + name: 'ExpressVPN', + } + } + ], + + // Solo App URL + [ + '/app/solo', + { + generateOptions: {}, + method: DoesItAPI => DoesItAPI.app('solo').url, + expected: result => result.includes( '/app/solo' ) + } + ], +] + + +test( 'API has valid responses', async t => { + // const { listingsDetails } = t.context + + for ( const [ caseEndpoint, listingCase ] of listingsCases ) { + + // const apiPath = listingsDetails[ caseEndpoint ].apiEndpointPath + + const DoesItAPI = generateAPI( listingCase.generateOptions ) + + const result = await listingCase.method( DoesItAPI ) + + // If expected is a function then call it + // Otherwise, compare the result to the expected + if ( typeof listingCase.expected === 'function' ) { + t.assert( listingCase.expected( result ), `API case method check for '${ caseEndpoint }'` ) + + continue + } + + t.like( result, listingCase.expected, `${ caseEndpoint } has a valid api endpoint` ) + } +})