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

Add unit tests for the ComputedProperty class #20826

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
"npm-run-all2": "^6.0.6",
"prettier": "^2.8.0",
"puppeteer": "^20.9.0",
"qunit": "^2.19.4",
"qunit": "^2.20.1",
"recast": "^0.22.0",
"rollup": "^4.2.0",
"rsvp": "^4.8.5",
Expand Down Expand Up @@ -401,4 +401,4 @@
"node": "16.20.0",
"pnpm": "8.10.0"
}
}
}
101 changes: 101 additions & 0 deletions packages/@ember/-internals/metal/tests/unit/computed-property-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { module, test } from 'qunit';
import EmberObject from '@ember/object';
import ComputedProperty from '@ember/-internals/metal/lib/computed.ts';
import { metaFor, tagMetaFor, tagFor, validateTag } from '@ember/-internals/metal/lib/tags';

module('Unit | ComputedProperty', function() {
test('get method - CT1', function(assert) {
let obj = EmberObject.create();
let keyName = 'testKey';
let computedProperty = new ComputedProperty(function() {
return 'testValue';
});

// Simulate conditions for CT1
let meta = metaFor(obj);
meta.setRevisionFor(keyName, undefined);
computedProperty._dependentKeys = undefined;

let propertyTag = tagFor(obj, keyName, tagMetaFor(obj));
validateTag(propertyTag, 0); // Simulate validateTag returning false

let result = computedProperty.get(obj, keyName);
assert.equal(result, undefined, 'Expected value should be undefined');
});

test('get method - CT2', function(assert) {
let obj = EmberObject.create();
let keyName = 'testKey';
let computedProperty = new ComputedProperty(function() {
return 'testValue';
});

// Simulate conditions for CT2
let meta = metaFor(obj);
meta.setRevisionFor(keyName, 1);
computedProperty._dependentKeys = undefined;

let propertyTag = tagFor(obj, keyName, tagMetaFor(obj));
validateTag(propertyTag, 0); // Simulate validateTag returning false

let result = computedProperty.get(obj, keyName);
assert.equal(result, 'testValue', 'Expected value should be returned');
});

test('get method - CT3', function(assert) {
let obj = EmberObject.create();
let keyName = 'testKey';
let computedProperty = new ComputedProperty(function() {
return 'testValue';
});

// Simulate conditions for CT3
let meta = metaFor(obj);
meta.setRevisionFor(keyName, undefined);
computedProperty._dependentKeys = undefined;

let propertyTag = tagFor(obj, keyName, tagMetaFor(obj));
validateTag(propertyTag, 1); // Simulate validateTag returning true

let result = computedProperty.get(obj, keyName);
assert.equal(result, 'testValue', 'Expected value should be returned');
});

test('get method - CT4', function(assert) {
let obj = EmberObject.create();
let keyName = 'testKey';
let computedProperty = new ComputedProperty(function() {
return 'testValue';
});

// Simulate conditions for CT4
let meta = metaFor(obj);
meta.setRevisionFor(keyName, undefined);
computedProperty._dependentKeys = ['depKey'];

let propertyTag = tagFor(obj, keyName, tagMetaFor(obj));
validateTag(propertyTag, 0); // Simulate validateTag returning false

let result = computedProperty.get(obj, keyName);
assert.equal(result, 'testValue', 'Expected value should be returned');
});

test('get method - CT5', function(assert) {
let obj = EmberObject.create();
let keyName = 'testKey';
let computedProperty = new ComputedProperty(function() {
return ['testValue'];
});

// Simulate conditions for CT5
let meta = metaFor(obj);
meta.setRevisionFor(keyName, undefined);
computedProperty._dependentKeys = undefined;

let propertyTag = tagFor(obj, keyName, tagMetaFor(obj));
validateTag(propertyTag, 0); // Simulate validateTag returning false

let result = computedProperty.get(obj, keyName);
assert.deepEqual(result, ['testValue'], 'Expected value should be returned');
});
});
4 changes: 4 additions & 0 deletions packages/@ember/-internals/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"type": "module",
"private": true,
"exports": {
"./metal/lib/computed": "./metal/lib/computed.js",
"./metal/lib/tags": "./metal/lib/tags.js",
"./browser-environment": "./browser-environment/index.ts",
"./container": "./container/index.ts",
"./environment": "./environment/index.ts",
Expand Down Expand Up @@ -70,4 +72,6 @@
"devDependencies": {
"@ember/template-compiler": "workspace:*"
}

}

Loading