Skip to content
Closed
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
3 changes: 3 additions & 0 deletions packages/identity-obj-proxy/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [ "es2015", "stage-0" ],
}
9 changes: 9 additions & 0 deletions packages/identity-obj-proxy/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "airbnb-base",
"env": {
"browser": true,
"node": true,
"es6": true,
"jest": true,
}
}
29 changes: 29 additions & 0 deletions packages/identity-obj-proxy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

.DS_Store
22 changes: 22 additions & 0 deletions packages/identity-obj-proxy/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
language: node_js

node_js:
- 6.2.0
- 5.3.0
- 4.0

compiler: clang-3.6

env:
- CXX=clang-3.6

addons:
apt:
sources:
- llvm-toolchain-precise-3.6
- ubuntu-toolchain-r-test
packages:
- clang-3.6
- g++-4.8

after_success: npm run coveralls
22 changes: 22 additions & 0 deletions packages/identity-obj-proxy/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Keyan Zhang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

51 changes: 51 additions & 0 deletions packages/identity-obj-proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# identity-obj-proxy [![Build Status](https://img.shields.io/travis/keyanzhang/identity-obj-proxy.svg?style=flat-square)](https://travis-ci.org/keyanzhang/identity-obj-proxy) [![npm version](https://img.shields.io/npm/v/identity-obj-proxy.svg?style=flat-square)](https://www.npmjs.com/package/identity-obj-proxy) [![test coverage](https://img.shields.io/coveralls/keyanzhang/identity-obj-proxy/master.svg?style=flat-square)](https://coveralls.io/github/keyanzhang/identity-obj-proxy?branch=master)
An identity object using ES6 proxies. Useful for mocking webpack imports. For instance, you can tell Jest to mock this object as imported [CSS modules](https://github.com/css-modules/css-modules); then all your `className` lookups on the imported `styles` object will be returned as-is.

```
npm install identity-obj-proxy
```

## ~~Real world example~~ Wait what does that even mean
### tl;dr
For a React component like
```js
import React, { Component } from 'react';

import styles from './App.css'; // CSS Modules here

export default class App extends Component {
render() {
return (
<div className={styles.root}>
<h1 className={styles.hello}>Hello, world!</h1>
</div>
);
}
}
```

we can generate a snapshot as below (notice that the class names get correctly mocked):
```js
exports[`test App renders correctly 1`] = `
<div
className="root">
<h1
className="hello">
Hello, world!
</h1>
</div>
`;
```

For more information, please take a look at https://github.com/keyanzhang/jest-css-modules-example/ and https://jestjs.io/docs/en/webpack.html.

## Requirement
No flag is required for Node.js `v6.*`; use `node --harmony_proxies` flag for `v5.*` and `v4.*`.

## Example
``` javascript
import idObj from 'identity-obj-proxy';
console.log(idObj.foo); // 'foo'
console.log(idObj.bar); // 'bar'
console.log(idObj[1]); // '1'
```
55 changes: 55 additions & 0 deletions packages/identity-obj-proxy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "@opensourceframework/identity-obj-proxy",
"version": "3.0.0",
"description": "an identity object using ES6 proxies (forked)",
"main": "src/index.js",
"scripts": {
"lint": "eslint src",
"test": "node --harmony_proxies node_modules/.bin/jest",
"coverage": "node --harmony_proxies node_modules/.bin/jest --coverage",
"coveralls": "npm run coverage && cat ./coverage/lcov.info | coveralls",
"prepublish": "npm run lint && npm run test"
},
"engines": {
"node": ">=4"
},
"repository": {
"type": "git",
"url": "git+https://github.com/riceharvest/opensourceframework.git",
"directory": "packages/identity-obj-proxy"
},
"keywords": [
"proxy",
"proxies",
"identity",
"jest",
"mock"
],
"author": "OpenSource Framework Contributors (fork), Original: Keyan Zhang",
"license": "MIT",
"bugs": {
"url": "https://github.com/riceharvest/opensourceframework/issues?q=is%3Aissue+is%3Aopen+identity-obj-proxy"
},
"homepage": "https://github.com/riceharvest/opensourceframework/tree/main/packages/identity-obj-proxy#readme",
"opensourceframework": "*",
"dependencies": {
"harmony-reflect": "^1.4.6"
},
"devDependencies": {
"babel-core": "^6.11.4",
"babel-jest": "^14.1.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-stage-0": "^6.5.0",
"coveralls": "^2.11.12",
"eslint": "^3.2.2",
"eslint-config-airbnb-base": "^5.0.1",
"eslint-plugin-import": "^1.12.0",
"jest-cli": "^14.1.0"
},
"jest": {
"automock": false,
"testPathDirs": [
"<rootDir>/src"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import idObj from '../test-redirections/idObjES6Export';

describe('idObj', () => {
it('should return the key as a string', () => {
expect(idObj.foo).toBe('foo');
});
it('should support dot notation', () => {
expect(idObj.bar).toBe('bar');
});
it('should support bracket notation', () => {
expect(idObj[1]).toBe('1');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import idObj from '../test-redirections/idObjES6ImportExport';

describe('idObj', () => {
it('should return the key as a string', () => {
expect(idObj.foo).toBe('foo');
});
it('should support dot notation', () => {
expect(idObj.bar).toBe('bar');
});
it('should support bracket notation', () => {
expect(idObj[1]).toBe('1');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import idObj from '../test-redirections/idObjES6Import';

describe('idObj', () => {
it('should return the key as a string', () => {
expect(idObj.foo).toBe('foo');
});
it('should support dot notation', () => {
expect(idObj.bar).toBe('bar');
});
it('should support bracket notation', () => {
expect(idObj[1]).toBe('1');
});
});
13 changes: 13 additions & 0 deletions packages/identity-obj-proxy/src/__tests__/import-vanilla-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import idObj from '..';

describe('idObj', () => {
it('should return the key as a string', () => {
expect(idObj.foo).toBe('foo');
});
it('should support dot notation', () => {
expect(idObj.bar).toBe('bar');
});
it('should support bracket notation', () => {
expect(idObj[1]).toBe('1');
});
});
13 changes: 13 additions & 0 deletions packages/identity-obj-proxy/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const idObj = require('..');

describe('idObj', () => {
it('should return the key as a string', () => {
expect(idObj.foo).toBe('foo');
});
it('should support dot notation', () => {
expect(idObj.bar).toBe('bar');
});
it('should support bracket notation', () => {
expect(idObj[1]).toBe('1');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const idObj = require('../test-redirections/idObjES6Export').default;

describe('idObj', () => {
it('should return the key as a string', () => {
expect(idObj.foo).toBe('foo');
});
it('should support dot notation', () => {
expect(idObj.bar).toBe('bar');
});
it('should support bracket notation', () => {
expect(idObj[1]).toBe('1');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const idObj = require('../test-redirections/idObjES6ImportExport').default;

describe('idObj', () => {
it('should return the key as a string', () => {
expect(idObj.foo).toBe('foo');
});
it('should support dot notation', () => {
expect(idObj.bar).toBe('bar');
});
it('should support bracket notation', () => {
expect(idObj[1]).toBe('1');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const idObj = require('../test-redirections/idObjES6Import');

describe('idObj', () => {
it('should return the key as a string', () => {
expect(idObj.foo).toBe('foo');
});
it('should support dot notation', () => {
expect(idObj.bar).toBe('bar');
});
it('should support bracket notation', () => {
expect(idObj[1]).toBe('1');
});
});
13 changes: 13 additions & 0 deletions packages/identity-obj-proxy/src/__tests__/require-vanilla-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const idObj = require('..');

describe('idObj', () => {
it('should return the key as a string', () => {
expect(idObj.foo).toBe('foo');
});
it('should support dot notation', () => {
expect(idObj.bar).toBe('bar');
});
it('should support bracket notation', () => {
expect(idObj[1]).toBe('1');
});
});
26 changes: 26 additions & 0 deletions packages/identity-obj-proxy/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable no-var, comma-dangle */
var Reflect; // eslint-disable-line no-unused-vars
var idObj;

function checkIsNodeV6OrAbove() {
if (typeof process === 'undefined') {
return false;
}

return parseInt(process.versions.node.split('.')[0], 10) >= 6;
}

if (!checkIsNodeV6OrAbove()) {
Reflect = require('harmony-reflect'); // eslint-disable-line global-require
}

idObj = new Proxy({}, {
get: function getter(target, key) {
if (key === '__esModule') {
return false;
}
return key;
}
});

module.exports = idObj;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const idObj = require('..');

export default idObj;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import idObj from '..';

module.exports = idObj;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import idObj from '..';

export default idObj;
Loading