From e7e02f41a6f64c72c84eebb94295904a6eaed187 Mon Sep 17 00:00:00 2001 From: Kevin Miller Date: Thu, 9 Jan 2025 09:48:09 -0800 Subject: [PATCH] feat: track name as first field in a record --- CHANGELOG.md | 20 +++++++++++++++++++ package.json | 2 +- src/environment/sdk/__sdk.js | 7 ++++++- .../sdk/globals/base/record/index.ts | 12 ++++++----- .../sdk/globals/base/record/record.test.ts | 15 +++++++------- src/environment/sdk/globals/output.test.ts | 12 +++++------ 6 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e917da2 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,20 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [0.0.2] - 2025-01-09 + +### Changed + +- Changed the `name` property of records to pull data from the first field in the table. + +## [0.0.1] - 2025-01-08 + +### Added + +- Initial release. diff --git a/package.json b/package.json index 36af54e..83ba0ef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jest-environment-airtable-script", - "version": "0.0.1", + "version": "0.0.2", "description": "A jest environment for testing Airtable scripts in extensions and automations", "license": "Apache-2.0", "author": "", diff --git a/src/environment/sdk/__sdk.js b/src/environment/sdk/__sdk.js index 147bd42..36d0d8d 100644 --- a/src/environment/sdk/__sdk.js +++ b/src/environment/sdk/__sdk.js @@ -8531,11 +8531,16 @@ class Record { constructor(data, fields) { this.id = data.id; - this.name = data.name || ''; this._cellValues = data.cellValuesByFieldId; this._fields = fields; this._accessibleFields = fields; } + get name() { + if (!this._fields || !this._fields[0]) { + return ''; + } + return this.getCellValueAsString(this._fields[0]); + } /** * Gets a field ID based on whether an item is a Field object, field ID, or field name. * diff --git a/src/environment/sdk/globals/base/record/index.ts b/src/environment/sdk/globals/base/record/index.ts index f5e20a6..5181366 100644 --- a/src/environment/sdk/globals/base/record/index.ts +++ b/src/environment/sdk/globals/base/record/index.ts @@ -10,10 +10,6 @@ class Record { * The unique ID of this record. */ id: string - /** - * The primary cell value as a string, or 'Unnamed record' if primary cell value is empty. - */ - name: string /** * The raw cell values of this record. */ @@ -33,12 +29,18 @@ class Record { constructor(data: FixtureRecord, fields: Field[]) { this.id = data.id - this.name = data.name || '' this._cellValues = data.cellValuesByFieldId this._fields = fields this._accessibleFields = fields } + get name(): string { + if (!this._fields || !this._fields[0]) { + return '' + } + return this.getCellValueAsString(this._fields[0]) + } + /** * Gets a field ID based on whether an item is a Field object, field ID, or field name. * diff --git a/src/environment/sdk/globals/base/record/record.test.ts b/src/environment/sdk/globals/base/record/record.test.ts index 0470ffe..6a3e22a 100644 --- a/src/environment/sdk/globals/base/record/record.test.ts +++ b/src/environment/sdk/globals/base/record/record.test.ts @@ -9,7 +9,6 @@ describe('Record', () => { const data = { id: 'rec1', - name: 'Test Record', cellValuesByFieldId: { fld1: 'Value 1', fld2: 'Value 2', @@ -22,32 +21,32 @@ describe('Record', () => { record = new Record(data, fields) }) - test('should create a record with correct id and name', () => { + it('should create a record with correct id and name', () => { expect(record.id).toBe('rec1') - expect(record.name).toBe('Test Record') + expect(record.name).toBe('Value 1') }) - test('should get cell value by a Field class', () => { + it('should get cell value by a Field class', () => { expect(record.getCellValue(fields[0])).toBe('Value 1') expect(record.getCellValue(fields[1])).toBe('Value 2') }) - test('should get cell value by field id', () => { + it('should get cell value by field id', () => { expect(record.getCellValue('fld1')).toBe('Value 1') expect(record.getCellValue('fld2')).toBe('Value 2') }) - test('should get cell value by field name', () => { + it('should get cell value by field name', () => { expect(record.getCellValue('Field 1')).toBe('Value 1') expect(record.getCellValue('Field 2')).toBe('Value 2') }) - test('should get cell value as string', () => { + it('should get cell value as string', () => { expect(record.getCellValueAsString('fld1')).toBe('Value 1') expect(record.getCellValueAsString('fld2')).toBe('Value 2') }) - test('should throw error if field not found', () => { + it('should throw error if field not found', () => { expect(() => record.getCellValue('fld3')).toThrow( 'No field matching "fld3" found in table' ) diff --git a/src/environment/sdk/globals/output.test.ts b/src/environment/sdk/globals/output.test.ts index ec89c9e..857a6f6 100644 --- a/src/environment/sdk/globals/output.test.ts +++ b/src/environment/sdk/globals/output.test.ts @@ -6,7 +6,7 @@ describe('automationOutput', () => { __output.length = 0 }) - test('should set key and value', () => { + it('should set key and value', () => { automationOutput.set('key1', 'value1') expect(__output).toEqual([{ key: 'key1', value: 'value1' }]) }) @@ -17,17 +17,17 @@ describe('extensionOutput', () => { __output.length = 0 }) - test('should add text to output', () => { + it('should add text to output', () => { extensionOutput.text('some text') expect(__output).toEqual(['some text']) }) - test('should add markdown to output', () => { + it('should add markdown to output', () => { extensionOutput.markdown('**some** markdown') expect(__output).toEqual(['**some** markdown']) }) - test('should add table to output', () => { + it('should add table to output', () => { const table = [ ['a', 1], ['b', 2], @@ -38,13 +38,13 @@ describe('extensionOutput', () => { ]) }) - test('should add inspected object to output', () => { + it('should add inspected object to output', () => { const obj = { key: 'value' } extensionOutput.inspect(obj) expect(__output).toEqual([JSON.stringify(obj)]) }) - test('should clear the output', () => { + it('should clear the output', () => { extensionOutput.text('some text') extensionOutput.clear() expect(__output[0]).toEqual('some text')