mardi 23 juin 2020

Use Jest to Test Local JSON File?

I have a analytics application using React + Gatsby (Uses local JSON as data) and I have jest setup, but I can't figure out how to test for the following things:

  1. JSON file exists
  2. JSON file is JSON
  3. Last object in JSON property date_updated

Below is my test that I've been doing trial and error + My JSON file I'm trying to test.

Needs Clarification:

This is my first time doing testing, so the whole mock thing is new to me. Does the JSON file need to be mocked? If so, why?

data.test.js

// Imports: Dependencies
import React from 'react';
import renderer from 'react-test-renderer';

// Imports: JSON File
import orangeCountyCovid19Data from '../data/orangeCountyCovid19Data.json';

// Test: Orange County Data
describe('Data: Orange County', () => {

  // Last Day
  const lastDay = orangeCountyCovid19Data[orangeCountyCovid19Data.length - 1];

  console.log(lastDay)

  // Data Should Be JSON
  // expect(orangeCountyCovid19Data).toBe(JSON);

  // Data Property: date_updated
  // expect(lastDay).toHaveProperty('date_updated');


  // expect(orangeCountyCovid19Data).toBe(true);

  // jest.mock('../data/orangeCountyCovid19Data.json', ()=>({
  //   settings: 'someSetting'
  // }), { virtual: true })
});

orangeCountyCovid19Data.json:

[
    {
        "date_updated": "06/23/2020",
        "number_of_people_tested": null,
        "new_tests": null,
        "number_of_people_kits_can_test": null,
        "cases": {
            "new_cases": 147,
            "total_cases": 10737,
            "total_cases_male": 5383,
            "total_cases_female": 5331,
            "total_cases_0_to_17": 533,
            "total_cases_18_to_24": 1147,
            "total_cases_25_to_34": 2074,
            "total_cases_35_to_44": 1710,
            "total_cases_45_to_54": 1859,
            "total_cases_55_to_64": 1505,
            "total_cases_65_to_74": 854,
            "total_cases_75_to_84": 588,
            "total_cases_over_84": 465
        },
        "cases_by_city": {
            "aliso_viejo": 50,
            "anaheim": 2060,
            "brea": 76,
            "buena_park": 334,
            "costa_mesa": 231,
            "coto_de_caza": 8,
            "cypress": 99,
            "dana_point": 41,
            "fountain_valley": 91,
            "fullerton": 416,
            "garden_grove": 619,
            "huntington_beach": 495,
            "irvine": 283,
            "la_habra": 214,
            "la_palma": 32,
            "ladera_ranch": 23,
            "laguna_beach": 54,
            "laguna_hills": 55,
            "laguna_niguel": 60,
            "laguna_woods": 15,
            "lake_forest": 110,
            "los_alamitos": 88,
            "midway_city": 31,
            "mission_viejo": 119,
            "newport_beach": 204,
            "orange": 471,
            "placentia": 195,
            "rancho_mission_viejo": 12,
            "rancho_santa_margarita": 43,
            "rossmoor": 14,
            "san_clemente": 82,
            "san_juan_capistrano": 75,
            "santa_ana": 2260,
            "seal_beach": 92,
            "stanton": 140,
            "trabuco_canyon": 22,
            "tustin": 199,
            "villa_park": 13,
            "westminster": 227,
            "yorba_linda": 126,
            "other": 394,
            "unknown": 564
        },
        "deaths": {
            "total_deaths": 273,
            "total_deaths_male": 155,
            "total_deaths_female": 118,
            "total_deaths_0_to_17": 0,
            "total_deaths_18_to_24": 0,
            "total_deaths_25_to_34": 3,
            "total_deaths_35_to_44": 7,
            "total_deaths_45_to_54": 24,
            "total_deaths_55_to_64": 37,
            "total_deaths_65_to_74": 48,
            "total_deaths_75_to_84": 70,
            "total_deaths_over_84": 84
        },
        "hospitalized": {
            "hospitals_reporting": null,
            "total_hospitalized": null,
            "total_icu": null
        },
        "travel_related": {
            "travel_related": null
        },
        "person_to_person": {
            "person_to_person": null
        },
        "community_acquired": {
            "community_acquired": null
        },
        "under_investigation": {
            "under_investigation": null
        }
    }
]

Aucun commentaire:

Enregistrer un commentaire