From 2fb5a437a27b756d61e413fcbd9e8699bdb126af Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Tue, 1 Nov 2022 14:16:22 -0800 Subject: [PATCH] feat: add additional currencies (#125) Add additional currencies and ensure Frontend/Backend currencies are synched via testing --- Taskfile.yml | 2 +- backend/go.sum | 4 ++++ backend/internal/data/ent/group/group.go | 4 +++- backend/internal/data/ent/migrate/schema.go | 2 +- backend/internal/data/ent/schema/group.go | 2 +- frontend/lib/api/__test__/user/group.test.ts | 15 +++++++++++++++ frontend/lib/data/currency.ts | 14 +++++++++++++- frontend/test/vitest.config.ts | 7 +++++++ 8 files changed, 45 insertions(+), 5 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index 51a756b..3dd05de 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -100,7 +100,7 @@ tasks: ui:watch: desc: Starts the vitest test runner in watch mode cmds: - - cd frontend && pnpm vitest --watch + - cd frontend && pnpm run test:watch ui:dev: desc: Run frontend development server diff --git a/backend/go.sum b/backend/go.sum index af688c1..a16fd64 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -72,11 +72,13 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -88,6 +90,8 @@ github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY= github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0= github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= +github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/backend/internal/data/ent/group/group.go b/backend/internal/data/ent/group/group.go index f46cc94..d6596a9 100644 --- a/backend/internal/data/ent/group/group.go +++ b/backend/internal/data/ent/group/group.go @@ -124,6 +124,8 @@ const ( CurrencyEur Currency = "eur" CurrencyGbp Currency = "gbp" CurrencyJpy Currency = "jpy" + CurrencyZar Currency = "zar" + CurrencyAud Currency = "aud" ) func (c Currency) String() string { @@ -133,7 +135,7 @@ func (c Currency) String() string { // CurrencyValidator is a validator for the "currency" field enum values. It is called by the builders before save. func CurrencyValidator(c Currency) error { switch c { - case CurrencyUsd, CurrencyEur, CurrencyGbp, CurrencyJpy: + case CurrencyUsd, CurrencyEur, CurrencyGbp, CurrencyJpy, CurrencyZar, CurrencyAud: return nil default: return fmt.Errorf("group: invalid enum value for currency field: %q", c) diff --git a/backend/internal/data/ent/migrate/schema.go b/backend/internal/data/ent/migrate/schema.go index aeb751e..4e59c20 100644 --- a/backend/internal/data/ent/migrate/schema.go +++ b/backend/internal/data/ent/migrate/schema.go @@ -127,7 +127,7 @@ var ( {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "name", Type: field.TypeString, Size: 255}, - {Name: "currency", Type: field.TypeEnum, Enums: []string{"usd", "eur", "gbp", "jpy"}, Default: "usd"}, + {Name: "currency", Type: field.TypeEnum, Enums: []string{"usd", "eur", "gbp", "jpy", "zar", "aud"}, Default: "usd"}, } // GroupsTable holds the schema information for the "groups" table. GroupsTable = &schema.Table{ diff --git a/backend/internal/data/ent/schema/group.go b/backend/internal/data/ent/schema/group.go index efd706c..33dbb1e 100644 --- a/backend/internal/data/ent/schema/group.go +++ b/backend/internal/data/ent/schema/group.go @@ -27,7 +27,7 @@ func (Group) Fields() []ent.Field { NotEmpty(), field.Enum("currency"). Default("usd"). - Values("usd", "eur", "gbp", "jpy"), // TODO: add more currencies + Values("usd", "eur", "gbp", "jpy", "zar", "aud"), } } diff --git a/frontend/lib/api/__test__/user/group.test.ts b/frontend/lib/api/__test__/user/group.test.ts index 93aa576..181cfd5 100644 --- a/frontend/lib/api/__test__/user/group.test.ts +++ b/frontend/lib/api/__test__/user/group.test.ts @@ -2,6 +2,7 @@ import { faker } from "@faker-js/faker"; import { describe, test, expect } from "vitest"; import { factories } from "../factories"; import { sharedUserClient } from "../test-utils"; +import { currencies } from "~~/lib/data/currency"; describe("first time user workflow (register, login, join group)", () => { test("user should be able to update group", async () => { @@ -28,6 +29,20 @@ describe("first time user workflow (register, login, join group)", () => { expect(group.currency).toBe("USD"); }); + test("currencies should be in sync with backend", async () => { + const { client } = await factories.client.singleUse(); + + for (const currency of currencies) { + const { response, data: group } = await client.group.update({ + name: faker.name.firstName(), + currency: currency.code, + }); + + expect(response.status).toBe(200); + expect(group.currency).toBe(currency.code); + } + }); + test("user should be able to join create join token and have user signup", async () => { const api = factories.client.public(); diff --git a/frontend/lib/data/currency.ts b/frontend/lib/data/currency.ts index 8643d96..688b789 100644 --- a/frontend/lib/data/currency.ts +++ b/frontend/lib/data/currency.ts @@ -1,4 +1,4 @@ -export type Codes = "USD" | "EUR" | "GBP" | "JPY"; +export type Codes = "USD" | "EUR" | "GBP" | "JPY" | "ZAR" | "AUD"; export type Currency = { code: Codes; @@ -32,4 +32,16 @@ export const currencies: Currency[] = [ symbol: "¥", name: "Japanese Yen", }, + { + code: "ZAR", + local: "en-ZA", + symbol: "R", + name: "South African Rand", + }, + { + code: "AUD", + local: "en-AU", + symbol: "$", + name: "Australian Dollar", + }, ]; diff --git a/frontend/test/vitest.config.ts b/frontend/test/vitest.config.ts index e20e192..11fe560 100644 --- a/frontend/test/vitest.config.ts +++ b/frontend/test/vitest.config.ts @@ -1,7 +1,14 @@ +import path from "path"; import { defineConfig } from "vite"; export default defineConfig({ test: { globalSetup: "./test/setup.ts", }, + resolve: { + alias: { + "@": path.resolve(__dirname, ".."), + "~~": path.resolve(__dirname, ".."), + }, + }, });