Skip to content

Commit

Permalink
test: fix test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Aug 20, 2024
1 parent 29b9dc2 commit ef530be
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 75 deletions.
140 changes: 65 additions & 75 deletions packages/@ourworldindata/utils/src/MultiDimDataPageConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,90 @@
#! /usr/bin/env jest

import { MultiDimDataPageConfigPreProcessed } from "@ourworldindata/types"
import { MultiDimDataPageConfig } from "./MultiDimDataPageConfig.js"
import YAML from "yaml"

it("fromObject", () => {
const config = MultiDimDataPageConfig.fromObject({ title: "Test" } as any)
expect(config.config.title).toBe("Test")
})

const CONFIG: MultiDimDataPageConfigPreProcessed = {
title: {
title: "Anything goes",
},
dimensions: [
{
slug: "view",
name: "View",
choices: [
{
slug: "stunting",
name: "Stunting",
},
{
slug: "poverty",
name: "Poverty",
},
],
},
{
slug: "interval",
name: "Time interval",
choices: [
{
slug: "yearly",
name: "Yearly",
},
{
slug: "weekly",
name: "Weekly",
},
],
},
],
views: [
{
dimensions: {
view: "stunting",
interval: "yearly",
},
indicators: {
y: [111, 222],
},
},
{
dimensions: {
view: "poverty",
interval: "yearly",
},
indicators: {
y: [819727],
},
},
],
}

describe("methods", () => {
const yaml = `
name: CO₂ emissions
dimensions_title: by fuel type and sector
common_indicator_path_prefix: gcp/2024/co2_and_ghg_emissions/ # optional
dimensions:
- slug: fuel_type
name: Fuel type
description: The fuel where these emissions stem from. # optional
multi_select: true # optional
choices:
- slug: gas
name: Natural gas
description: Natural gas, e.g. methane or propane.
- slug: cement
name: Cement
description: Emissions from the drying-out of cement.
- slug: all
name: All
description: Emissions from all fuel types.
multi_select: false
- slug: sector
name: Sector
description: The sector where these emissions stem from.
choices:
- slug: agriculture
name: Agriculture
description: Agricultural emissions, e.g. from land use change and emissions from livestock and harvesting.
- slug: transport
name: Transportation
description: Emissions from global transport, including cars, trucks, rail transport and global shipping.
- slug: industry
name: Industry
description: Emissions from the production of non-agricultural goods, e.g. steel and glass.
- slug: heating
- slug: electricity
views:
- dimensions:
fuel_type: gas
sector: transport
indicators:
emissions__gas__transport: y
config:
title: Natural gas emissions in transport
- dimensions:
fuel_type: oil
sector: transport
indicators:
emissions__oil__transport: y
config:
title: Oil emissions in transport
subtitle: ...
- dimensions: # This dimension uses other fields; they probably are nicer to work with
fuel_type: solar
sector: electricity
indicators:
emissions__solar__transport: y
config:
title: ...
subtitle: ...
`
const config = MultiDimDataPageConfig.fromObject(YAML.parse(yaml))
const config = MultiDimDataPageConfig.fromObject(CONFIG)

it("dimensions", () => {
expect(Object.keys(config.dimensions)).toEqual(["fuel_type", "sector"])
expect(Object.keys(config.dimensions["fuel_type"].choices)).toEqual([
"gas",
"cement",
"all",
expect(Object.keys(config.dimensions)).toEqual(["view", "interval"])
expect(Object.keys(config.dimensions["view"].choicesBySlug)).toEqual([
"stunting",
"poverty",
])
})

it("filterViewsByDimensions", () => {
const views = config.filterViewsByDimensions({
sector: "transport",
view: "stunting",
})
expect(views).toHaveLength(2)
expect(views.map((v) => v.config?.title)).toEqual([
"Natural gas emissions in transport",
"Oil emissions in transport",
])
expect(views).toHaveLength(1)
})

it("findViewByDimensions", () => {
const view = config.findViewByDimensions({
fuel_type: "gas",
sector: "transport",
view: "stunting",
})
expect(view).toBeDefined()
})
Expand Down
9 changes: 9 additions & 0 deletions public/multi-dim/mixed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ dimensions:
name: Stunting
- slug: poverty
name: Poverty
- slug: interval
name: Time interval
choices:
- slug: yearly
name: Yearly
- slug: weekly
name: Weekly

views:
- dimensions:
view: stunting
interval: yearly
indicators:
y: grapher/worldbank_wdi/2024-05-20/wdi/wdi#sh_sta_stnt_zs
- dimensions:
view: poverty
interval: yearly
indicators:
y: 819727

0 comments on commit ef530be

Please sign in to comment.