Permalink
Please sign in to comment.
Showing
with
118 additions
and 11 deletions.
- +10 −1 .babelrc
- +13 −0 client/state/selectors/README.md
- +14 −0 client/state/selectors/index.js
- +53 −0 client/state/selectors/test/index.js
- +13 −5 docs/our-approach-to-data.md
- +14 −5 npm-shrinkwrap.json
- +1 −0 package.json
| @@ -0,0 +1,13 @@ | ||
| +State Selectors | ||
| +=============== | ||
| + | ||
| +This folder contains all available state selectors. Each file includes a single default exported function which can be used as a helper in retrieving derived data from the global state tree. | ||
| + | ||
| +To learn more about selectors, refer to the ["Our Approach to Data" document](../../../docs/our-approach-to-data.md#selectors). | ||
| + | ||
| +When adding a new selector to this directory, make note of the following details: | ||
| + | ||
| +- Each new selector exists in its own file, named with [kebab case](https://en.wikipedia.org/wiki/Kebab_case) (dash-delimited lowercase) | ||
| +- There should be no more than a single default exported function per selector file | ||
| +- Tests for each selector should exist in the [`test/` subdirectory](./test) with matching file name of the selector | ||
| +- Your selector must be exported from [`index.js`](./index.js) to enable named importing from the base `state/selectors` directory |
| @@ -0,0 +1,14 @@ | ||
| +/** | ||
| + * Every selector contained within this directory should have its default | ||
| + * export included in the list below. Please keep this list alphabetized for | ||
| + * easy scanning. | ||
| + * | ||
| + * For more information about how we use selectors, refer to the docs: | ||
| + * - https://wpcalypso.wordpress.com/devdocs/docs/our-approach-to-data.md#selectors | ||
| + * | ||
| + * Studious observers may note that our project is not configured to support | ||
| + * "tree shaking", and that importing from this file might unnecessarily bloat | ||
| + * the size of bundles. Fear not! For we do not truly import from this file, | ||
| + * but instead use a Babel plugin "transform-imports" to transform the import | ||
| + * to its individual file. | ||
| + */ |
| @@ -0,0 +1,53 @@ | ||
| +/** | ||
| + * External dependencies | ||
| + */ | ||
| +import { expect } from 'chai'; | ||
| +import kebabCase from 'lodash/kebabCase'; | ||
| +import camelCase from 'lodash/camelCase'; | ||
| +import each from 'lodash/each'; | ||
| +import fs from 'fs'; | ||
| +import path from 'path'; | ||
| + | ||
| +/** | ||
| + * Internal dependencies | ||
| + */ | ||
| +import * as selectors from '../'; | ||
| + | ||
| +/** | ||
| + * Constants | ||
| + */ | ||
| + | ||
| +/** | ||
| + * Matches string which ends in ".js" file extension | ||
| + * | ||
| + * @type {RegExp} | ||
| + */ | ||
| +const RX_JS_EXTENSION = /\.js$/; | ||
| + | ||
| +describe( 'selectors', () => { | ||
| + it( 'should match every selector to its default export', () => { | ||
| + each( selectors, ( selector, key ) => { | ||
| + expect( require( '../' + kebabCase( key ) ) ).to.equal( selector ); | ||
| + } ); | ||
| + } ); | ||
| + | ||
| + it( 'should export every selector file', ( done ) => { | ||
| + fs.readdir( path.join( __dirname, '..' ), ( error, files ) => { | ||
| + if ( error ) { | ||
| + return done( error ); | ||
| + } | ||
| + | ||
| + each( files, ( file ) => { | ||
| + if ( ! RX_JS_EXTENSION.test( file ) || 'index.js' === file ) { | ||
| + return; | ||
| + } | ||
| + | ||
| + const exportName = camelCase( file.replace( RX_JS_EXTENSION, '' ) ); | ||
| + const errorMessage = `Expected to find selector \`${ exportName }\` (${ file }) in index.js exports.`; | ||
| + expect( selectors, errorMessage ).to.include.keys( exportName ); | ||
| + } ); | ||
| + | ||
| + done(); | ||
| + } ); | ||
| + } ); | ||
| +} ); |
0 comments on commit
ea4ed92