From 60a7083a73c43cbfa584920c4261aa570b01c03d Mon Sep 17 00:00:00 2001 From: yunchipang Date: Tue, 5 Nov 2024 17:52:39 -0800 Subject: [PATCH] chore: undo api endpoints change --- .env.development | 4 +-- .../fixtures/capture.json | 0 .../integration/captures/[captureid].cy.js | 32 ++++++++++++++++--- src/models/api.js | 6 +++- src/models/apiPaths.js | 10 +++--- src/pages/captures/[captureid].js | 4 +-- 6 files changed, 42 insertions(+), 14 deletions(-) rename doc/examples/captures/0c2d6e3a-d02a-4edf-bcdc-893e1f7a4817.json => cypress/fixtures/capture.json (100%) diff --git a/.env.development b/.env.development index 791cbcee..63614238 100644 --- a/.env.development +++ b/.env.development @@ -1,5 +1,5 @@ -NEXT_PUBLIC_API=https://dev-k8s.treetracker.org/query/ -NEXT_PUBLIC_API_V2=https://dev-k8s.treetracker.org/query/v2 +NEXT_PUBLIC_API=https://dev-k8s.treetracker.org/query/v2 +NEXT_PUBLIC_API_V2=https://dev-k8s.treetracker.org/query/ NEXT_PUBLIC_STAKEHOLDER_API=https://dev-k8s.treetracker.org/stakeholder NEXT_PUBLIC_TILE_SERVER_URL=https://{s}.treetracker.org/tiles/ NEXT_PUBLIC_TILE_SERVER_SUBDOMAINS=dev-k8s diff --git a/doc/examples/captures/0c2d6e3a-d02a-4edf-bcdc-893e1f7a4817.json b/cypress/fixtures/capture.json similarity index 100% rename from doc/examples/captures/0c2d6e3a-d02a-4edf-bcdc-893e1f7a4817.json rename to cypress/fixtures/capture.json diff --git a/cypress/tests/integration/captures/[captureid].cy.js b/cypress/tests/integration/captures/[captureid].cy.js index 2b279e7c..f6ae554e 100644 --- a/cypress/tests/integration/captures/[captureid].cy.js +++ b/cypress/tests/integration/captures/[captureid].cy.js @@ -1,8 +1,32 @@ -import capture from '../../../../doc/examples/captures/0c2d6e3a-d02a-4edf-bcdc-893e1f7a4817.json'; -import { prepareNocks, clearNocks } from '../nockRoutes'; +import capture from '../../../fixtures/capture.json'; beforeEach(() => { - clearNocks(); + if (Cypress.env('nock')) { + cy.task('clearNock'); + } }); -describe('Captures', () => {}); +it('getStaticProps returns mock data for capture', () => { + const path = `/captures/${capture.id}`; + + if (Cypress.env('nock')) { + cy.task('nock', { + hostname: Cypress.env('NEXT_PUBLIC_API'), + method: 'GET', + path: `/query/v2/captures/${capture.id}`, + statusCode: 200, + body: capture, + }); + } + + cy.visit(path); + + // Assertions + cy.contains(`Capture #${capture.id}`); + cy.contains(capture.species_name || 'Unknown Species'); + cy.contains( + `Captured on ${new Date(capture.created_at).toLocaleDateString()}`, + ); + cy.contains(capture.token_id ? 'Token issued' : 'Token not issued'); + cy.contains(capture.wallet_name || 'No wallet owns it'); +}); diff --git a/src/models/api.js b/src/models/api.js index dfcd31de..33f799af 100644 --- a/src/models/api.js +++ b/src/models/api.js @@ -157,9 +157,13 @@ export async function getTreeById(id) { } } -export async function getCaptureById(id) { +export async function getCapturesById(id) { try { const url = apiPaths.captures(id); + + // debug + log.warn('---------- getCapturesById url: ', url); + const begin = Date.now(); const res = await axios.get(url); const { data } = res; diff --git a/src/models/apiPaths.js b/src/models/apiPaths.js index 1ef20f7c..708eac06 100644 --- a/src/models/apiPaths.js +++ b/src/models/apiPaths.js @@ -5,16 +5,16 @@ const hostV2 = process.env.NEXT_PUBLIC_API_V2 || ''; const hostStakeholder = process.env.NEXT_PUBLIC_STAKEHOLDER_API || ''; const apiPaths = { featuredTrees: urlJoin(host, 'trees/featured'), - featuredGrowers: urlJoin(hostV2, 'growers/featured'), - getCaptures: urlJoin(hostV2, 'captures'), + featuredGrowers: urlJoin(host, 'growers/featured'), + getCaptures: urlJoin(host, 'captures'), countriesLatLon: (lat = '', lon = '') => urlJoin(host, `/countries?lat=${lat}&lon=${lon}`), leaders: urlJoin(host, '/countries/leaderboard'), trees: (id = '') => urlJoin(host, `/trees/${id}`), getCapturesByTreeId: (treeid = '') => - urlJoin(hostV2, `/captures?tree_id=${treeid}`), - captures: (id = '') => urlJoin(hostV2, `captures/${id}`), - growers: (id = '') => urlJoin(hostV2, `growers/${id}`), + urlJoin(host, `/captures?tree_id=${treeid}`), + captures: (id = '') => urlJoin(host, `captures/${id}`), + growers: (id = '') => urlJoin(host, `growers/${id}`), stakeHolders: (id = '') => urlJoin(hostStakeholder, `/stakeholders/${id}`), species: urlJoin(host, '/species'), organization: (id = '') => urlJoin(host, `/organizations/${id}`), diff --git a/src/pages/captures/[captureid].js b/src/pages/captures/[captureid].js index b3552426..61d7f3fc 100644 --- a/src/pages/captures/[captureid].js +++ b/src/pages/captures/[captureid].js @@ -34,7 +34,7 @@ import SearchIcon from 'images/search.svg'; import { useMapContext } from 'mapContext'; import { getStakeHolderById, - getCaptureById, + getCapturesById, getGrowerById, getCountryByLatLon, } from 'models/api'; @@ -739,7 +739,7 @@ export default function Capture({ async function serverSideData(params) { const { captureid } = params; - const capture = await getCaptureById(captureid); + const capture = await getCapturesById(captureid); const { planting_organization_id, grower_account_id, lat, lon } = capture; // fetch grower and country information