Skip to content

Commit

Permalink
Clean up ember-utils package's tests
Browse files Browse the repository at this point in the history
  • Loading branch information
locks committed Dec 18, 2017
1 parent 5cbfbad commit 7218a48
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 244 deletions.
75 changes: 39 additions & 36 deletions packages/ember-utils/tests/assign_test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
import { assignPolyfill as assign } from '..';

QUnit.module('Ember.assign');

QUnit.test('merging objects', function() {
let trgt = { a: 1 };
let src1 = { b: 2 };
let src2 = { c: 3 };

assign(trgt, src1, src2);

deepEqual(trgt, { a: 1, b: 2, c: 3 }, 'assign copies values from one or more source objects to a target object');
deepEqual(src1, { b: 2 }, 'assign does not change source object 1');
deepEqual(src2, { c: 3 }, 'assign does not change source object 2');
});

QUnit.test('merging objects with same property', function() {
let trgt = { a: 1, b: 1 };
let src1 = { a: 2, b: 2 };
let src2 = { a: 3 };

assign(trgt, src1, src2);
deepEqual(trgt, { a: 3, b: 2 }, 'properties are overwritten by other objects that have the same properties later in the parameters order');
});

QUnit.test('null', function() {
let trgt = { a: 1 };

assign(trgt, null);
deepEqual(trgt, { a: 1 }, 'null as a source parameter is ignored');
});

QUnit.test('undefined', function() {
let trgt = { a: 1 };

assign(trgt, null);
deepEqual(trgt, { a: 1 }, 'undefined as a source parameter is ignored');
import {
moduleFor,
AbstractTestCase as TestCase
} from 'internal-test-helpers';

moduleFor('Ember.assign', class extends TestCase {
['@test merging objects'](assert) {
let trgt = { a: 1 };
let src1 = { b: 2 };
let src2 = { c: 3 };
assign(trgt, src1, src2);

assert.deepEqual(trgt, { a: 1, b: 2, c: 3 }, 'assign copies values from one or more source objects to a target object');
assert.deepEqual(src1, { b: 2 }, 'assign does not change source object 1');
assert.deepEqual(src2, { c: 3 }, 'assign does not change source object 2');
}

['@test merging objects with same property'](assert) {
let trgt = { a: 1, b: 1 };
let src1 = { a: 2, b: 2 };
let src2 = { a: 3 };
assign(trgt, src1, src2);

assert.deepEqual(trgt, { a: 3, b: 2 }, 'properties are overwritten by other objects that have the same properties later in the parameters order');
}

['@test null'](assert) {
let trgt = { a: 1 };
assign(trgt, null);

assert.deepEqual(trgt, { a: 1 }, 'null as a source parameter is ignored');
}

['@test undefined'](assert) {
let trgt = { a: 1 };
assign(trgt, null);

assert.deepEqual(trgt, { a: 1 }, 'undefined as a source parameter is ignored');
}
});
47 changes: 27 additions & 20 deletions packages/ember-utils/tests/can_invoke_test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
import { canInvoke } from '..';
import {
moduleFor,
AbstractTestCase as TestCase
} from 'internal-test-helpers';

let obj;

QUnit.module('Ember.canInvoke', {
setup() {
moduleFor('Ember.canInvoke', class extends TestCase {
constructor() {
super();

obj = {
foobar: 'foobar',
aMethodThatExists() {}
};
},
}

teardown() {
obj = undefined;
}
});

QUnit.test('should return false if the object doesn\'t exist', function() {
equal(canInvoke(undefined, 'aMethodThatDoesNotExist'), false);
});
['@test should return false if the object doesn\'t exist'](assert) {
assert.equal(canInvoke(undefined, 'aMethodThatDoesNotExist'), false);
}

QUnit.test('should return true for falsy values that have methods', function() {
equal(canInvoke(false, 'valueOf'), true);
equal(canInvoke('', 'charAt'), true);
equal(canInvoke(0, 'toFixed'), true);
});
['@test should return true for falsy values that have methods'](assert) {
assert.equal(canInvoke(false, 'valueOf'), true);
assert.equal(canInvoke('', 'charAt'), true);
assert.equal(canInvoke(0, 'toFixed'), true);
}

QUnit.test('should return true if the method exists on the object', function() {
equal(canInvoke(obj, 'aMethodThatExists'), true);
});
['@test should return true if the method exists on the object'](assert) {
assert.equal(canInvoke(obj, 'aMethodThatExists'), true);
}

QUnit.test('should return false if the method doesn\'t exist on the object', function() {
equal(canInvoke(obj, 'aMethodThatDoesNotExist'), false);
});
['@test should return false if the method doesn\'t exist on the object'](assert) {
assert.equal(canInvoke(obj, 'aMethodThatDoesNotExist'), false);
}

QUnit.test('should return false if the property exists on the object but is a non-function', function() {
equal(canInvoke(obj, 'foobar'), false);
['@test should return false if the property exists on the object but is a non-function'](assert) {
assert.equal(canInvoke(obj, 'foobar'), false);
}
});

12 changes: 8 additions & 4 deletions packages/ember-utils/tests/checkHasSuper_test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { environment } from 'ember-environment';
import { checkHasSuper } from '..';

QUnit.module('checkHasSuper');
import {
moduleFor,
AbstractTestCase as TestCase
} from 'internal-test-helpers';

// Only run this test on browsers that we are certain should have function
// source available. This allows the test suite to continue to pass on other
// platforms that correctly (for them) fall back to the "always wrap" code.
if (environment.isChrome || environment.isFirefox) {
QUnit.test('does not super wrap needlessly [GH #12462]', function(assert) {
assert.notOk(checkHasSuper(function() {}), 'empty function does not have super');
moduleFor('checkHasSuper', class extends TestCase {
['@test does not super wrap needlessly [GH #12462]'](assert) {
assert.notOk(checkHasSuper(function() {}), 'empty function does not have super');
}
});
}
14 changes: 9 additions & 5 deletions packages/ember-utils/tests/generate_guid_test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { generateGuid } from '..';
import {
moduleFor,
AbstractTestCase as TestCase
} from 'internal-test-helpers';

QUnit.module('Ember.generateGuid');
moduleFor('Ember.generateGuid', class extends TestCase {
['@test Prefix'](assert) {
let a = {};

QUnit.test('Prefix', function() {
let a = {};

ok(generateGuid(a, 'tyrell').indexOf('tyrell') > -1, 'guid can be prefixed');
assert.ok(generateGuid(a, 'tyrell').indexOf('tyrell') > -1, 'guid can be prefixed');
}
});
150 changes: 77 additions & 73 deletions packages/ember-utils/tests/guid_for_test.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,89 @@
import {
guidFor
} from '..';
import {
moduleFor,
AbstractTestCase as TestCase
} from 'internal-test-helpers';

QUnit.module('guidFor');

function sameGuid(a, b, message) {
equal(guidFor(a), guidFor(b), message);
function sameGuid(assert, a, b, message) {
assert.equal(guidFor(a), guidFor(b), message);
}

function diffGuid(a, b, message) {
ok(guidFor(a) !== guidFor(b), message);
function diffGuid(assert, a, b, message) {
assert.ok(guidFor(a) !== guidFor(b), message);
}

function nanGuid(obj) {
function nanGuid(assert, obj) {
let type = typeof obj;
ok(isNaN(parseInt(guidFor(obj), 0)), 'guids for ' + type + 'don\'t parse to numbers');
assert.ok(isNaN(parseInt(guidFor(obj), 0)), 'guids for ' + type + 'don\'t parse to numbers');
}

QUnit.test('Object', function() {
let a = {};
let b = {};

sameGuid(a, a, 'same object always yields same guid');
diffGuid(a, b, 'different objects yield different guids');
nanGuid(a);
});

QUnit.test('strings', function() {
let a = 'string A';
let aprime = 'string A';
let b = 'String B';

sameGuid(a, a, 'same string always yields same guid');
sameGuid(a, aprime, 'identical strings always yield the same guid');
diffGuid(a, b, 'different strings yield different guids');
nanGuid(a);
});

QUnit.test('numbers', function() {
let a = 23;
let aprime = 23;
let b = 34;

sameGuid(a, a, 'same numbers always yields same guid');
sameGuid(a, aprime, 'identical numbers always yield the same guid');
diffGuid(a, b, 'different numbers yield different guids');
nanGuid(a);
});

QUnit.test('numbers', function() {
let a = true;
let aprime = true;
let b = false;

sameGuid(a, a, 'same booleans always yields same guid');
sameGuid(a, aprime, 'identical booleans always yield the same guid');
diffGuid(a, b, 'different boolean yield different guids');
nanGuid(a);
nanGuid(b);
});

QUnit.test('null and undefined', function() {
let a = null;
let aprime = null;
let b;

sameGuid(a, a, 'null always returns the same guid');
sameGuid(b, b, 'undefined always returns the same guid');
sameGuid(a, aprime, 'different nulls return the same guid');
diffGuid(a, b, 'null and undefined return different guids');
nanGuid(a);
nanGuid(b);
});

QUnit.test('arrays', function() {
let a = ['a', 'b', 'c'];
let aprime = ['a', 'b', 'c'];
let b = ['1', '2', '3'];

sameGuid(a, a, 'same instance always yields same guid');
diffGuid(a, aprime, 'identical arrays always yield the same guid');
diffGuid(a, b, 'different arrays yield different guids');
nanGuid(a);
moduleFor('guidFor', class extends TestCase {
['@test Object'](assert) {
let a = {};
let b = {};

sameGuid(assert, a, a, 'same object always yields same guid');
diffGuid(assert, a, b, 'different objects yield different guids');
nanGuid(assert, a);
}

['@test strings'](assert) {
let a = 'string A';
let aprime = 'string A';
let b = 'String B';

sameGuid(assert, a, a, 'same string always yields same guid');
sameGuid(assert, a, aprime, 'identical strings always yield the same guid');
diffGuid(assert, a, b, 'different strings yield different guids');
nanGuid(assert, a);
}

['@test numbers'](assert) {
let a = 23;
let aprime = 23;
let b = 34;

sameGuid(assert, a, a, 'same numbers always yields same guid');
sameGuid(assert, a, aprime, 'identical numbers always yield the same guid');
diffGuid(assert, a, b, 'different numbers yield different guids');
nanGuid(assert, a);
}

['@test numbers'](assert) {
let a = true;
let aprime = true;
let b = false;

sameGuid(assert, a, a, 'same booleans always yields same guid');
sameGuid(assert, a, aprime, 'identical booleans always yield the same guid');
diffGuid(assert, a, b, 'different boolean yield different guids');
nanGuid(assert, a);
nanGuid(assert, b);
}

['@test null and undefined'](assert) {
let a = null;
let aprime = null;
let b;

sameGuid(assert, a, a, 'null always returns the same guid');
sameGuid(assert, b, b, 'undefined always returns the same guid');
sameGuid(assert, a, aprime, 'different nulls return the same guid');
diffGuid(assert, a, b, 'null and undefined return different guids');
nanGuid(assert, a);
nanGuid(assert, b);
}

['@test arrays'](assert) {
let a = ['a', 'b', 'c'];
let aprime = ['a', 'b', 'c'];
let b = ['1', '2', '3'];

sameGuid(assert, a, a, 'same instance always yields same guid');
diffGuid(assert, a, aprime, 'identical arrays always yield the same guid');
diffGuid(assert, a, b, 'different arrays yield different guids');
nanGuid(assert, a);
}
});
Loading

0 comments on commit 7218a48

Please sign in to comment.