Skip to content

Commit

Permalink
Merge pull request #638 from simonihmig/collection-preserve-getters
Browse files Browse the repository at this point in the history
Support string getters within collections
  • Loading branch information
ro0gr committed Feb 2, 2024
2 parents 28b1d8b + 0822699 commit 752b6a2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
10 changes: 10 additions & 0 deletions addon/src/-private/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,13 @@ export function findClosestValue(node, property) {
return findClosestValue(parent, property);
}
}

export function assignDescriptors(target, source) {
Object.getOwnPropertyNames(source).forEach((key) => {
const descriptor = Object.getOwnPropertyDescriptor(source, key);

Object.defineProperty(target, key, descriptor);
});

return target;
}
11 changes: 1 addition & 10 deletions addon/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@ import {
} from './-private/meta';
import dsl from './-private/dsl';
import { getter } from './macros/index';

function assignDescriptors(target, source) {
Object.getOwnPropertyNames(source).forEach((key) => {
const descriptor = Object.getOwnPropertyDescriptor(source, key);

Object.defineProperty(target, key, descriptor);
});

return target;
}
import { assignDescriptors } from './-private/helpers';

//
// When running RFC268 tests, we have to play some tricks to support chaining.
Expand Down
4 changes: 2 additions & 2 deletions addon/src/properties/collection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Ceibo from '@ro0gr/ceibo';
import { buildSelector } from '../-private/helpers';
import { buildSelector, assignDescriptors } from '../-private/helpers';
import { isPageObject, getPageObjectDefinition } from '../-private/meta';
import { create } from '../create';
import { count } from './count';
Expand Down Expand Up @@ -199,7 +199,7 @@ export class Collection {
let { scope, definition, parent } = this;
let itemScope = buildSelector({}, scope, { at: index });

let finalizedDefinition = { ...definition };
let finalizedDefinition = assignDescriptors({}, definition);

finalizedDefinition.scope = itemScope;

Expand Down
51 changes: 50 additions & 1 deletion test-app/tests/integration/string-properties-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { module, test } from 'qunit';
import { create } from 'ember-cli-page-object';
import { collection, create } from 'ember-cli-page-object';

module('string-properties', function () {
test('throws in top-level string property', function (assert) {
Expand All @@ -21,6 +21,21 @@ Key: "stringProp"`)
}),
new Error(`string values are not supported in page object definitions
Key: "stringProp"`)
);
});

test('throws in collection string property', function (assert) {
const po = create({
items: collection('.items', {
stringProp: '',
}),
});

assert.throws(
() => po.items[0]?.stringProp,
new Error(`string values are not supported in page object definitions
Key: "stringProp"`)
);
});
Expand All @@ -40,4 +55,38 @@ Key: "stringProp"`)

assert.true(true);
});

test('allows native getter', function (assert) {
const po = create({
get foo() {
return 'bar';
},
});

assert.strictEqual(po.foo, 'bar');
});

test('allows nested native getter', function (assert) {
const po = create({
nested: {
get foo() {
return 'bar';
},
},
});

assert.strictEqual(po.nested.foo, 'bar');
});

test('allows native getter in collection', function (assert) {
const po = create({
items: collection('.items', {
get foo() {
return 'bar';
},
}),
});

assert.strictEqual(po.items[0]?.foo, 'bar');
});
});

0 comments on commit 752b6a2

Please sign in to comment.