Skip to content

Commit

Permalink
Merge pull request #3 from evan-king/browser-support
Browse files Browse the repository at this point in the history
More flexible handling of immutability testing.  Exception added to p…
  • Loading branch information
Evan King committed Jun 19, 2016
2 parents 80e95af + eb54f8e commit bc4e0e0
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,44 @@ describe('PrimitiveEnum instances', function() {
});

they('are immutable', function() {
expect(function() { mapEnum.c.d = 'q'; }).throw();
expect(function() { mapEnum.key = 'q'; }).throw();
expect(function() { mapEnum.value = 'q'; }).throw();
expect(function() { mapEnum.defaultKey = 'q'; }).throw();
expect(function() { mapEnum.defaultValue = 'q'; }).throw();

expect(function() { mapEnum.c = 'q'; }).throw();
expect(function() { mapEnum.map.c = 'q'; }).throw();
expect(function() { mapEnum.keys.c = 'q'; }).throw();
expect(function() { mapEnum.values.c = 'q'; }).throw();
expect(function() { mapEnum.reverseMap.c = 'q'; }).throw();

expect(function() { mapEnum.d = 'q'; }).throw();
expect(function() { mapEnum.map.d = 'q'; }).throw();
expect(function() { mapEnum.keys.d = 'q'; }).throw();
expect(function() { mapEnum.values.d = 'q'; }).throw();
expect(function() { mapEnum.reverseMap.d = 'q'; }).throw();
let throwCount = 0,
expectCount = 0,
mutEnum = Enum({a: 'x', c: 'z', b: 'y'});

function tryModify(baseObj, prop) {
let val = 'blah';
try {
expectCount++;
baseObj[prop] = val;
} catch(ex) {
throwCount++;
expect(ex instanceof TypeError).true;
}
expect(baseObj[prop]).not.eql(val);
}

tryModify(mutEnum, 'key');
tryModify(mutEnum, 'value');
tryModify(mutEnum, 'defaultKey');
tryModify(mutEnum, 'defaultValue');

tryModify(mutEnum, 'c');
tryModify(mutEnum.map, 'c');
tryModify(mutEnum.keys, 'c');
tryModify(mutEnum.values, 'c');
tryModify(mutEnum.reverseMap, 'c');

tryModify(mutEnum, 'd');
tryModify(mutEnum.map, 'd');
tryModify(mutEnum.keys, 'd');
tryModify(mutEnum.values, 'd');
tryModify(mutEnum.reverseMap, 'd');

expect(throwCount).eql(expectCount);

// Browsers do not consistently throw an error for this case,
// but aren't necessessarily accepting the assignment either.
tryModify(mapEnum.c, 'd', 'q');
});

they('are serializable', function() {
Expand Down

0 comments on commit bc4e0e0

Please sign in to comment.