Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: track name as first field in a record #1

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": "",
Expand Down
7 changes: 6 additions & 1 deletion src/environment/sdk/__sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
12 changes: 7 additions & 5 deletions src/environment/sdk/globals/base/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
*
Expand Down
15 changes: 7 additions & 8 deletions src/environment/sdk/globals/base/record/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ describe('Record', () => {

const data = {
id: 'rec1',
name: 'Test Record',
cellValuesByFieldId: {
fld1: 'Value 1',
fld2: 'Value 2',
Expand All @@ -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'
)
Expand Down
12 changes: 6 additions & 6 deletions src/environment/sdk/globals/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }])
})
Expand All @@ -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],
Expand All @@ -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')
Expand Down
Loading