Skip to content

Commit

Permalink
[enzyme-adapter-utils] [new] add getMaskedContext
Browse files Browse the repository at this point in the history
  • Loading branch information
minznerjosh authored and ljharb committed Jan 8, 2019
1 parent a325b81 commit 30749f6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/enzyme-adapter-utils/package.json
Expand Up @@ -36,6 +36,7 @@
"dependencies": {
"function.prototype.name": "^1.1.0",
"object.assign": "^4.1.0",
"object.fromentries": "^2.0.0",
"prop-types": "^15.6.2",
"semver": "^5.6.0"
},
Expand Down
8 changes: 8 additions & 0 deletions packages/enzyme-adapter-utils/src/Utils.js
@@ -1,4 +1,5 @@
import functionName from 'function.prototype.name';
import fromEntries from 'object.fromentries';
import createMountWrapper from './createMountWrapper';
import createRenderWrapper from './createRenderWrapper';
import wrap from './wrapWithSimpleWrapper';
Expand Down Expand Up @@ -279,3 +280,10 @@ export function simulateError(

componentDidCatch.call(catchingInstance, error, { componentStack });
}

export function getMaskedContext(contextTypes, unmaskedContext) {
if (!contextTypes || !unmaskedContext) {
return {};
}
return fromEntries(Object.keys(contextTypes).map(key => [key, unmaskedContext[key]]));
}
33 changes: 33 additions & 0 deletions packages/enzyme-test-suite/test/adapter-utils-spec.jsx
Expand Up @@ -3,6 +3,7 @@ import { expect } from 'chai';
import {
displayNameOfNode,
ensureKeyOrUndefined,
getMaskedContext,
} from 'enzyme-adapter-utils';

import './_helpers/setupAdapters';
Expand Down Expand Up @@ -82,4 +83,36 @@ describe('enzyme-adapter-utils', () => {
});
});
});

describe('getMaskedContext', () => {
const contextTypes = {
a() {},
c() {},
};
const unmaskedContext = {
a: 1,
b: 2,
c: 3,
};
const falsies = [undefined, null, false, '', NaN, 0];

it('returns an empty object with falsy `contextTypes`', () => {
falsies.forEach((falsy) => {
expect(getMaskedContext(falsy, unmaskedContext)).to.eql({});
});
});

it('returns an empty object with falsy `unmaskedContext`', () => {
falsies.forEach((falsy) => {
expect(getMaskedContext(contextTypes, falsy)).to.eql({});
});
});

it('filters `unmaskedContext` down to `contextTypes`', () => {
expect(getMaskedContext(contextTypes, unmaskedContext)).to.eql({
a: unmaskedContext.a,
c: unmaskedContext.c,
});
});
});
});

0 comments on commit 30749f6

Please sign in to comment.