From 879fd3206d2b41cf8739d4c904afa73c3196384a Mon Sep 17 00:00:00 2001 From: George Enciu Date: Sat, 8 Jul 2023 22:47:04 +0200 Subject: [PATCH] format write #7 --- .github/workflows/deploy.yml | 7 +- .github/workflows/pr.yml | 4 +- .../src/firestore/delete-collection.ts | 8 +- .../src/actions/emit-training-event.test.ts | 111 ++++++++++-------- libs/training-events/test/setup-test.ts | 9 +- nx.json | 6 +- tsconfig.base.json | 22 +--- 7 files changed, 93 insertions(+), 74 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index cfa5b58..d0b580d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -2,9 +2,9 @@ name: Build and deploy on: pull_request: types: - - "closed" + - 'closed' branches: - - "master" + - 'master' jobs: main: @@ -16,6 +16,9 @@ jobs: - uses: nrwl/nx-set-shas@v3 with: main-branch-name: 'master' + - uses: actions/setup-node@v3 + with: + node-version: 18 - run: npm ci - run: npx nx format:check diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 0121717..a1c6012 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -2,7 +2,6 @@ name: Run on PR on: pull_request: - jobs: main: runs-on: ubuntu-latest @@ -13,6 +12,9 @@ jobs: - uses: nrwl/nx-set-shas@v3 with: main-branch-name: 'master' + - uses: actions/setup-node@v3 + with: + node-version: 20 - run: npm ci - run: npx nx format:check diff --git a/libs/test-helpers/src/firestore/delete-collection.ts b/libs/test-helpers/src/firestore/delete-collection.ts index f51aa7e..98747ce 100644 --- a/libs/test-helpers/src/firestore/delete-collection.ts +++ b/libs/test-helpers/src/firestore/delete-collection.ts @@ -1,7 +1,10 @@ export async function deleteCollection(collectionPath: string) { - const endpoint = process.env['FIRESTORE_EMULATOR_HOST'] as string - const project = process.env['GCLOUD_PROJECT'] as string + const endpoint = process.env['FIRESTORE_EMULATOR_HOST'] as string; + const project = process.env['GCLOUD_PROJECT'] as string; + console.log( + `http://${endpoint}/emulator/v1/projects/${project}/databases/(default)/documents/${collectionPath}` + ); const response = await fetch( `http://${endpoint}/emulator/v1/projects/${project}/databases/(default)/documents/${collectionPath}`, { @@ -12,4 +15,3 @@ export async function deleteCollection(collectionPath: string) { throw new Error('Trouble clearing Emulator: ' + (await response.text())); } } - diff --git a/libs/training-events/src/actions/emit-training-event.test.ts b/libs/training-events/src/actions/emit-training-event.test.ts index 789d12b..d4b660d 100644 --- a/libs/training-events/src/actions/emit-training-event.test.ts +++ b/libs/training-events/src/actions/emit-training-event.test.ts @@ -1,40 +1,43 @@ -import { afterEach, beforeEach, describe, it, expect } from "vitest"; -import { buildTrainingIntentEvent } from '@rovacc/test-helpers' +import { afterEach, beforeEach, describe, it, expect } from 'vitest'; +import { buildTrainingIntentEvent } from '@rovacc/test-helpers'; -import { emitTrainingEvent } from "./emit-training-event"; -import { deleteCollection } from "@rovacc/test-helpers"; -import { getDatabaseCollection } from "@rovacc/clients"; -import { Timestamp } from "firebase-admin/firestore"; +import { emitTrainingEvent } from './emit-training-event'; +import { deleteCollection } from '@rovacc/test-helpers'; +import { getDatabaseCollection } from '@rovacc/clients'; +import { Timestamp } from 'firebase-admin/firestore'; - -const TRAINING_ID = 'trainingId' -const EVENT_ID = 'eventId' -const DATE = new Date() +const TRAINING_ID = 'trainingId'; +const EVENT_ID = 'eventId'; +const DATE = new Date(); describe('emitTrainingEvent', () => { - beforeEach(async () => { - await deleteCollection('training') + await deleteCollection('training'); vi.mock('uuid', () => ({ - v4: () => EVENT_ID - })) + v4: () => EVENT_ID, + })); vi.mock('../helpers/get-date', () => ({ - getDate: () => DATE - })) - }) + getDate: () => DATE, + })); + }); afterEach(async () => { - vi.restoreAllMocks() - }) + vi.restoreAllMocks(); + }); it('should emit the event correctly and create the empty training object', async () => { const eventData = buildTrainingIntentEvent({ trainingId: TRAINING_ID, emittedAt: DATE, - eventId: EVENT_ID - }) + eventId: EVENT_ID, + }); - const reducedTraining = await emitTrainingEvent(eventData, null, 'correlationId') + const reducedTraining = await emitTrainingEvent( + eventData, + null, + 'correlationId' + ); + console.log({ reducedTraining }); expect(reducedTraining).toEqual({ trainingId: TRAINING_ID, @@ -42,10 +45,14 @@ describe('emitTrainingEvent', () => { purpose: 'acquire_rating', rating: 2, student: 123123123, - requestedAt: DATE - }) - const trainingCollection = getDatabaseCollection('training') - const event = await trainingCollection.doc(TRAINING_ID).collection('events').doc(EVENT_ID).get() + requestedAt: DATE, + }); + const trainingCollection = getDatabaseCollection('training'); + const event = await trainingCollection + .doc(TRAINING_ID) + .collection('events') + .doc(EVENT_ID) + .get(); expect(event.data()).toEqual({ eventId: EVENT_ID, @@ -54,18 +61,22 @@ describe('emitTrainingEvent', () => { trainingId: TRAINING_ID, payload: { student: 123123123, purpose: 'acquire_rating', rating: 2 }, name: 'training-intent', - correlationId: 'correlationId' - }) - }) + correlationId: 'correlationId', + }); + }); it('should not emit the same event twice', async () => { const eventData1 = buildTrainingIntentEvent({ trainingId: TRAINING_ID, emittedAt: DATE, - eventId: EVENT_ID - }) + eventId: EVENT_ID, + }); - const reducedTraining1 = await emitTrainingEvent(eventData1, null, 'correlationId') + const reducedTraining1 = await emitTrainingEvent( + eventData1, + null, + 'correlationId' + ); expect(reducedTraining1).toEqual({ trainingId: TRAINING_ID, @@ -73,10 +84,14 @@ describe('emitTrainingEvent', () => { purpose: 'acquire_rating', rating: 2, student: 123123123, - requestedAt: DATE - }) - const trainingCollection = getDatabaseCollection('training') - const event1 = await trainingCollection.doc(TRAINING_ID).collection('events').doc(EVENT_ID).get() + requestedAt: DATE, + }); + const trainingCollection = getDatabaseCollection('training'); + const event1 = await trainingCollection + .doc(TRAINING_ID) + .collection('events') + .doc(EVENT_ID) + .get(); expect(event1.data()).toEqual({ eventId: EVENT_ID, @@ -85,17 +100,21 @@ describe('emitTrainingEvent', () => { trainingId: TRAINING_ID, payload: { student: 123123123, purpose: 'acquire_rating', rating: 2 }, name: 'training-intent', - correlationId: 'correlationId' - }) + correlationId: 'correlationId', + }); const eventData2 = buildTrainingIntentEvent({ trainingId: TRAINING_ID, emittedAt: DATE, - eventId: 'eventId2' - }) - await emitTrainingEvent(eventData2, reducedTraining1, 'correlationId2') - const eventCount = await trainingCollection.doc(TRAINING_ID).collection('events').count().get() - - expect(eventCount.data().count).toEqual(1) - }) -}) + eventId: 'eventId2', + }); + await emitTrainingEvent(eventData2, reducedTraining1, 'correlationId2'); + const eventCount = await trainingCollection + .doc(TRAINING_ID) + .collection('events') + .count() + .get(); + + expect(eventCount.data().count).toEqual(1); + }); +}); diff --git a/libs/training-events/test/setup-test.ts b/libs/training-events/test/setup-test.ts index 17f0e01..a8bea31 100644 --- a/libs/training-events/test/setup-test.ts +++ b/libs/training-events/test/setup-test.ts @@ -1,6 +1,7 @@ -import { initializeApp } from "firebase-admin"; -import { beforeAll } from "vitest"; +import { initializeApp } from 'firebase-admin'; +import { beforeAll } from 'vitest'; beforeAll(() => { - initializeApp() -}) + console.log('initializing app'); + initializeApp(); +}); diff --git a/nx.json b/nx.json index f85708b..c2cb096 100644 --- a/nx.json +++ b/nx.json @@ -21,7 +21,11 @@ "inputs": ["default", "^production"] }, "lint": { - "inputs": ["default", "{workspaceRoot}/.eslintrc.json", "{workspaceRoot}/.eslintignore"] + "inputs": [ + "default", + "{workspaceRoot}/.eslintrc.json", + "{workspaceRoot}/.eslintignore" + ] } }, "namedInputs": { diff --git a/tsconfig.base.json b/tsconfig.base.json index 5957936..1deb724 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -10,30 +10,18 @@ "importHelpers": true, "target": "es2015", "module": "esnext", - "lib": [ - "es2020", - "dom" - ], + "lib": ["es2020", "dom"], "skipLibCheck": true, "skipDefaultLibCheck": true, "baseUrl": ".", "paths": { - "@rovacc/clients": [ - "libs/clients/src/index.ts" - ], - "@rovacc/test-helpers": [ - "libs/test-helpers/src/index.ts" - ], - "@rovacc/training-events": [ - "libs/training-events/src/index.ts" - ], + "@rovacc/clients": ["libs/clients/src/index.ts"], + "@rovacc/test-helpers": ["libs/test-helpers/src/index.ts"], + "@rovacc/training-events": ["libs/training-events/src/index.ts"], "@rovacc/training-events-types": [ "libs/training-events-types/src/index.ts" ] } }, - "exclude": [ - "node_modules", - "tmp" - ] + "exclude": ["node_modules", "tmp"] }