Repository: astorije/chai-immutable Branch: master Commit: 7be3ba70de16 Files: 17 Total size: 99.1 KB Directory structure: gitextract_3143o6vx/ ├── .eslintrc.yml ├── .gitignore ├── .npmrc ├── .nycrc ├── .prettierignore ├── .prettierrc.yml ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── appveyor.yml ├── chai-immutable.js ├── package.json ├── renovate.json └── test/ ├── .eslintrc.yml ├── index.html └── test.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .eslintrc.yml ================================================ root: true env: commonjs: true es6: true extends: - eslint:recommended - prettier rules: ## Possible Errors for-direction: error no-prototype-builtins: error no-template-curly-in-string: error ## Best Practices array-callback-return: error block-scoped-var: error complexity: - error - 16 consistent-return: error curly: error default-case: error dot-notation: error eqeqeq: error guard-for-in: error no-alert: error no-caller: error no-div-regex: error no-empty-function: error no-empty-pattern: error no-eq-null: error no-eval: error no-extend-native: error no-extra-bind: error no-extra-label: error no-global-assign: error no-implicit-coercion: error no-implicit-globals: error no-implied-eval: error no-iterator: error no-labels: error no-lone-blocks: error no-loop-func: error no-multi-str: error no-new: error no-new-func: error no-new-wrappers: error no-octal-escape: error no-proto: error no-return-assign: error no-script-url: error no-self-compare: error no-sequences: error no-throw-literal: error no-unmodified-loop-condition: error no-unused-expressions: error no-useless-call: error no-useless-concat: error no-useless-return: error no-void: error no-with: error prefer-promise-reject-errors: error radix: error yoda: error ## Strict Mode strict: error ## Variables no-catch-shadow: error no-label-var: error no-shadow: error no-shadow-restricted-names: error no-undef-init: error no-undefined: error no-use-before-define: error ## Node.js and CommonJS callback-return: error handle-callback-err: error no-buffer-constructor: error no-mixed-requires: error no-new-require: error no-path-concat: error no-process-env: error no-process-exit: error ## Stylistic Issues camelcase: error capitalized-comments: - error - always - ignoreConsecutiveComments: true consistent-this: error func-name-matching: error func-style: - error - declaration - allowArrowFunctions: true id-match: error lines-between-class-members: error max-depth: error max-nested-callbacks: - error - max: 6 max-statements-per-line: error new-cap: error no-array-constructor: error no-bitwise: error no-continue: error no-lonely-if: error no-multi-assign: error no-negated-condition: error no-nested-ternary: error no-new-object: error no-underscore-dangle: - error - allowAfterThis: true no-unneeded-ternary: error one-var: - error - never operator-assignment: error padding-line-between-statements: error sort-vars: error spaced-comment: error ## ECMAScript 6 arrow-body-style: error no-duplicate-imports: error no-restricted-imports: error no-useless-computed-key: error no-useless-constructor: error no-useless-rename: error no-var: error object-shorthand: error prefer-arrow-callback: error prefer-const: error prefer-destructuring: error prefer-numeric-literals: error prefer-spread: error prefer-template: error sort-imports: error symbol-description: error ================================================ FILE: .gitignore ================================================ node_modules/ yarn-error.log .nyc_output/ coverage/ ================================================ FILE: .npmrc ================================================ save-exact = true ================================================ FILE: .nycrc ================================================ { "reporter": [ "lcov", "text-summary" ] } ================================================ FILE: .prettierignore ================================================ .nyc_output/ coverage/ ================================================ FILE: .prettierrc.yml ================================================ singleQuote: true trailingComma: es5 ================================================ FILE: .travis.yml ================================================ language: node_js # https://github.com/nodejs/Release node_js: - 12 # EOL: 2022-04-01 - 10 # EOL: 2021-04-01 os: - linux - osx matrix: include: - env: IMMUTABLE_VERSION=^4.0.0-rc install: - yarn install --frozen-lockfile before_script: - if [ "$IMMUTABLE_VERSION" ]; then yarn add --dev immutable@$IMMUTABLE_VERSION; fi after_script: - if [[ "$TRAVIS_OS_NAME" == "linux" && "$TRAVIS_NODE_VERSION" == "12" ]]; then yarn coveralls; fi notifications: email: on_success: never on_failure: always # Identifies `a.b.c-xxx.n` tags as pre-releases, and `a.b.c` as stable releases before_deploy: | function dist_tag() { if [[ "$TRAVIS_TAG" = *"-"* ]]; then echo "next" else echo "latest" fi } deploy: provider: npm tag: $(dist_tag) email: secure: bXNlycnAR6z+PawxKBB6fSKuoAQb61bH3gYhOD8yIP5Lx0zj6HHDj8Ze4jsE8X133RVVWUPONqw0D09b/w0ahWfRlY80/TzOlT6HgYHTLKuKZsgGrLDbSBBGeXzU4I+/Zuo3fJ7xGIUIRe4qrUELnUYNE7VuB1Un78xTmkuZ0cY= api_key: secure: lmkAwQspZnROe6Ev0QdV/9l1cN10BWMyc7BfkecNVr1Ab1G6Kws4LoczbJS/VJmLmvKaSMppLvmA+32pIu1XGa3ezjQQoobi1VPTH7RUGOY57y3japctsgI9JFXzFIESJo6lspe8blSHH7J6pyEJ0Vbl6gnDju9ploQWCawdh1g= on: node: 10 os: linux tags: true repo: astorije/chai-immutable # If the current release is a stable release, remove potential pre-release tag after_deploy: | if [ "$(dist_tag)" == "latest" ]; then npm dist-tag rm chai-immutable next || true fi ================================================ FILE: CODE_OF_CONDUCT.md ================================================ # Contributor Covenant Code of Conduct ## Our Pledge In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. ## Our Standards Examples of behavior that contributes to creating a positive environment include: - Using welcoming and inclusive language - Being respectful of differing viewpoints and experiences - Gracefully accepting constructive criticism - Focusing on what is best for the community - Showing empathy towards other community members Examples of unacceptable behavior by participants include: - The use of sexualized language or imagery and unwelcome sexual attention or advances - Trolling, insulting/derogatory comments, and personal or political attacks - Public or private harassment - Publishing others' private information, such as a physical or electronic address, without explicit permission - Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. ## Scope This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project owner at jeremie (at) astori (dot) fr. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] [homepage]: http://contributor-covenant.org [version]: http://contributor-covenant.org/version/1/4/ ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2015 Jérémie Astori 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. ================================================ FILE: README.md ================================================ [![npm Version](https://img.shields.io/npm/v/chai-immutable.svg)](https://npmjs.org/package/chai-immutable) [![License](https://img.shields.io/npm/l/chai-immutable.svg)](LICENSE) [![Build Status](https://travis-ci.org/astorije/chai-immutable.svg?branch=master)](https://travis-ci.org/astorije/chai-immutable) [![Build Status](https://ci.appveyor.com/api/projects/status/407ts84pq7wd4kt9/branch/master?svg=true)](https://ci.appveyor.com/project/astorije/chai-immutable/branch/master) [![Coverage Status](https://coveralls.io/repos/astorije/chai-immutable/badge.svg)](https://coveralls.io/r/astorije/chai-immutable) [![devDependencies Status](https://david-dm.org/astorije/chai-immutable/dev-status.svg)](https://david-dm.org/astorije/chai-immutable?type=dev) [![peerDependencies Status](https://david-dm.org/astorije/chai-immutable/peer-status.svg)](https://david-dm.org/astorije/chai-immutable?type=peer) # Chai Immutable This plugin provides a set of [Chai](http://chaijs.com/) assertions for [Facebook's Immutable library for JavaScript collections](http://facebook.github.io/immutable-js/). ## Assertions - BDD API Reference (Expect / Should) - [`.empty`](#empty) - [`.equal(collection)`](#equalcollection) - [`.referenceEqual(value)`](#referenceequalvalue) - [`.include(value)`](#includevalue) - [`.keys(key1[, key2[, ...]])`](#keyskey1-key2-) - [`.property(path[, val])`](#propertypath-val) - [`.size(value)`](#sizevalue) - TDD API Reference (Assert) - [`.equal(actual, expected)`](#equalactual-expected) - [`.referenceEqual(actual, expected)`](#referenceequalactual-expected) - [`.notEqual(actual, expected)`](#notequalactual-expected) - [`.notReferenceEqual(actual, expected)`](#notreferenceequalactual-expected) - [`.sizeOf(collection, length)`](#sizeofcollection-length) ## Installation ### Node.js Install via [npm](http://npmjs.org) or [yarn](https://yarnpkg.com/): ```bash npm install --save-dev chai-immutable yarn add --dev chai-immutable ``` You can then use this plugin as any other Chai plugins: ```js const chai = require('chai'); const chaiImmutable = require('chai-immutable'); chai.use(chaiImmutable); ``` ### ES6 syntax (needs Babel transpiling) ```js import chai from 'chai'; import chaiImmutable from 'chai-immutable'; chai.use(chaiImmutable); ``` ### In the browser Include this plugin after including Chai and Immutable. It will automatically plug in to Chai and be ready for use: ```html ``` ### Using `chai-immutable` with other plugins If you are using this plugin with [`chai-as-promised`](https://github.com/domenic/chai-as-promised/) or [`dirty-chai`](https://github.com/prodatakey/dirty-chai), note that `chai-immutable` must be loaded **before** any of them. For example: ```js const chai = require('chai'); const chaiAsPromised = require('chai-as-promised'); const chaiImmutable = require('chai-immutable'); const dirtyChai = require('dirty-chai'); const { expect } = chai; chai.use(chaiImmutable); chai.use(chaiAsPromised); chai.use(dirtyChai); const { List } = require('immutable'); /* ... */ expect(Promise.resolve(List.of(1, 2, 3))).to.eventually.have.size(3); expect(true).to.be.true(); ``` ## BDD API Reference (Expect / Should) ### .empty Asserts that the immutable collection is empty. ```js expect(List()).to.be.empty; expect(List.of(1, 2, 3)).to.not.be.empty; ``` ### .equal(collection) - **@param** _{ Collection }_ collection Asserts that the values of the target are equivalent to the values of `collection`. Aliases of Chai's original `equal` method are also supported. ```js const a = List.of(1, 2, 3); const b = List.of(1, 2, 3); expect(a).to.equal(b); ``` Immutable data structures should only contain other immutable data structures (unlike `Array`s and `Object`s) to be considered immutable and properly work against `.equal()`. See [issue #24](https://github.com/astorije/chai-immutable/issues/24) for more information. Also, note that `deep.equal` and `eql` are synonyms of `equal` when tested against immutable data structures, therefore they are aliases to `equal`. ### .referenceEqual(value) - **@param** _{Collection}_ value Asserts that the reference of the target is equivalent to the reference of `collection`. This method preserves the original behavior of Chai's `equal`. See [issue #210](https://github.com/astorije/chai-immutable/issues/210) for more details. ```js const a = List.of(1, 2, 3); const b = a; const c = List.of(1, 2, 3); expect(a).to.referenceEqual(b); expect(a).to.not.referenceEqual(c); ``` ### .include(value) - **@param** _{ Mixed }_ val The `include` and `contain` assertions can be used as either property based language chains or as methods to assert the inclusion of a value or subset in an immutable collection. When used as language chains, they toggle the `contains` flag for the `keys` assertion. Note that `deep.include` behaves exactly like `include` in the context of immutable data structures. ```js expect(new List([1, 2, 3])).to.include(2); expect(new List([1, 2, 3])).to.deep.include(2); expect(new Map({ foo: 'bar', hello: 'world' })).to.include('bar'); expect(new Map({ a: 1, b: 2, c: 3 })).to.include(new Map({ a: 1, b: 2 })); expect(new Map({ foo: 'bar', hello: 'world' })).to.include.keys('foo'); ``` ### .keys(key1[, key2[, ...]]) - **@param** _{ String... | Array | Object | Collection }_ key*N* Asserts that the target collection has the given keys. When the target is an object or array, keys can be provided as one or more string arguments, a single array argument, a single object argument, or an immutable collection. In the last 2 cases, only the keys in the given object/collection matter; the values are ignored. ```js expect(new Map({ foo: 1, bar: 2 })).to.have.all.keys('foo', 'bar'); expect(new Map({ foo: 1, bar: 2 })).to.have.all.keys(new List(['bar', 'foo'])); expect(new Map({ foo: 1, bar: 2 })).to.have.all.keys(new Set(['bar', 'foo'])); expect(new Map({ foo: 1, bar: 2 })).to.have.all.keys(new Stack(['bar', 'foo'])); expect(new List(['x', 'y'])).to.have.all.keys(0, 1); expect(new Map({ foo: 1, bar: 2 })).to.have.all.keys(['foo', 'bar']); expect(new List(['x', 'y'])).to.have.all.keys([0, 1]); // Values in the passed object are ignored: expect(new Map({ foo: 1, bar: 2 })).to.have.all.keys({ bar: 6, foo: 7 }); expect(new Map({ foo: 1, bar: 2 })).to.have.all.keys( new Map({ bar: 6, foo: 7 }) ); expect(new List(['x', 'y'])).to.have.all.keys({ 0: 4, 1: 5 }); ``` Note that `deep.property` behaves exactly like `property` in the context of immutable data structures. By default, the target must have all of the given keys and no more. Add `.any` earlier in the chain to only require that the target have at least one of the given keys. Also, add `.not` earlier in the chain to negate `.keys`. It's often best to add `.any` when negating `.keys`, and to use `.all` when asserting `.keys` without negation. When negating `.keys`, `.any` is preferred because `.not.any.keys` asserts exactly what's expected of the output, whereas `.not.all.keys` creates uncertain expectations. ```js // Recommended; asserts that target doesn't have any of the given keys expect(new Map({ a: 1, b: 2 })).to.not.have.any.keys('c', 'd'); // Not recommended; asserts that target doesn't have all of the given // keys but may or may not have some of them expect(new Map({ a: 1, b: 2 })).to.not.have.all.keys('c', 'd'); ``` When asserting `.keys` without negation, `.all` is preferred because `.all.keys` asserts exactly what's expected of the output, whereas `.any.keys` creates uncertain expectations. ```js // Recommended; asserts that target has all the given keys expect(new Map({ a: 1, b: 2 })).to.have.all.keys('a', 'b'); // Not recommended; asserts that target has at least one of the given // keys but may or may not have more of them expect(new Map({ a: 1, b: 2 })).to.have.any.keys('a', 'b'); ``` Note that `.all` is used by default when neither `.all` nor `.any` appear earlier in the chain. However, it's often best to add `.all` anyway because it improves readability. ```js // Both assertions are identical expect(new Map({ a: 1, b: 2 })).to.have.all.keys('a', 'b'); // Recommended expect(new Map({ a: 1, b: 2 })).to.have.keys('a', 'b'); // Not recommended ``` Add `.include` earlier in the chain to require that the target's keys be a superset of the expected keys, rather than identical sets. ```js // Target object's keys are a superset of ['a', 'b'] but not identical expect(new Map({ a: 1, b: 2, c: 3 })).to.include.all.keys('a', 'b'); expect(new Map({ a: 1, b: 2, c: 3 })).to.not.have.all.keys('a', 'b'); ``` However, if `.any` and `.include` are combined, only the `.any` takes effect. The `.include` is ignored in this case. ```js // Both assertions are identical expect(new Map({ a: 1 })).to.have.any.keys('a', 'b'); expect(new Map({ a: 1 })).to.include.any.keys('a', 'b'); ``` The alias `.key` can be used interchangeably with `.keys`. ```js expect(new Map({ foo: 1 })).to.have.key('foo'); ``` ### .property(path[, val]) - **@param** _{ String | Array | Iterable }_ path - **@param** _{ Mixed }_ val (optional) Asserts that the target has a property with the given `path`. ```js expect(new Map({ a: 1 })).to.have.property('a'); ``` When `val` is provided, `.property` also asserts that the property's value is equal to the given `val`. `val` can be an immutable collection. ```js expect(new Map({ a: 1 })).to.have.property('a', 1); ``` Note that `deep.property` behaves exactly like `property` in the context of immutable data structures. Add `.nested` earlier in the chain to enable dot- and bracket-notation when referencing nested properties. An immutable `List` can also be used as the starting point of a `nested.property`. ```js expect(Immutable.fromJS({ a: { b: ['x', 'y'] } })).to.have.nested.property( 'a.b[1]' ); expect(Immutable.fromJS({ a: { b: ['x', 'y'] } })).to.have.nested.property( 'a.b[1]', 'y' ); expect(Immutable.fromJS({ a: { b: ['x', 'y'] } })).to.have.nested.property( ['a', 'b', 1], 'y' ); expect(Immutable.fromJS({ a: { b: ['x', 'y'] } })).to.have.nested.property( new List(['a', 'b', 1]), 'y' ); ``` If `.` or `[]` are part of an actual property name, they can be escaped by adding two backslashes before them. ```js expect(Immutable.fromJS({ '.a': { '[b]': 'x' } })).to.have.nested.property( '\\.a.\\[b\\]' ); ``` Add `.not` earlier in the chain to negate `.property`. ```js expect(new Map({ a: 1 })).to.not.have.property('b'); ``` However, it's dangerous to negate `.property` when providing `val`. The problem is that it creates uncertain expectations by asserting that the target either doesn't have a property at the given `path`, or that it does have a property at the given key `path` but its value isn't equal to the given `val`. It's often best to identify the exact output that's expected, and then write an assertion that only accepts that exact output. When the target isn't expected to have a property at the given `path`, it's often best to assert exactly that. ```js expect(new Map({ b: 2 })).to.not.have.property('a'); // Recommended expect(new Map({ b: 2 })).to.not.have.property('a', 1); // Not recommended ``` When the target is expected to have a property at the given key `path`, it's often best to assert that the property has its expected value, rather than asserting that it doesn't have one of many unexpected values. ```js expect(new Map({ a: 3 })).to.have.property('a', 3); // Recommended expect(new Map({ a: 3 })).to.not.have.property('a', 1); // Not recommended ``` `.property` changes the target of any assertions that follow in the chain to be the value of the property from the original target object. ```js expect(new Map({ a: 1 })) .to.have.property('a') .that.is.a('number'); ``` ### .size(value) - **@param** _{ Number }_ size Asserts that the immutable collection has the expected size. ```js expect(List.of(1, 2, 3)).to.have.size(3); ``` It can also be used as a chain precursor to a value comparison for the `size` property. ```js expect(List.of(1, 2, 3)).to.have.size.least(3); expect(List.of(1, 2, 3)).to.have.size.most(3); expect(List.of(1, 2, 3)).to.have.size.above(2); expect(List.of(1, 2, 3)).to.have.size.below(4); expect(List.of(1, 2, 3)).to.have.size.within(2, 4); ``` Similarly to `length`/`lengthOf`, `sizeOf` is an alias of `size`: ```js expect(List.of(1, 2, 3)).to.have.sizeOf(3); ``` ## TDD API Reference (Assert) ### .equal(actual, expected) - **@param** _{ Collection }_ actual - **@param** _{ Collection }_ expected Asserts that the values of `actual` are equivalent to the values of `expected`. Note that `.strictEqual()` and `.deepEqual()` assert exactly like `.equal()` in the context of Immutable data structures. ```js const a = List.of(1, 2, 3); const b = List.of(1, 2, 3); assert.equal(a, b); ``` Immutable data structures should only contain other immutable data structures (unlike `Array`s and `Object`s) to be considered immutable and properly work against `.equal()`, `.strictEqual()` or `.deepEqual()`. See [issue #24](https://github.com/astorije/chai-immutable/issues/24) for more information. ### .referenceEqual(actual, expected) - **@param** _{Collection}_ actual - **@param** _{Collection}_ expected Asserts that the reference of `actual` is equivalent to the reference of `expected`. This method preserves the original behavior of Chai's `equal`. See [issue #210](https://github.com/astorije/chai-immutable/issues/210) for more details. ```js const a = List.of(1, 2, 3); const b = a; const c = List.of(1, 2, 3); assert.referenceEqual(a, b); assert.throws(() => assert.referenceEqual(a, c)); ``` ### .notEqual(actual, expected) - **@param** _{ Collection }_ actual - **@param** _{ Collection }_ expected Asserts that the values of `actual` are not equivalent to the values of `expected`. Note that `.notStrictEqual()` and `.notDeepEqual()` assert exactly like `.notEqual()` in the context of Immutable data structures. ```js const a = List.of(1, 2, 3); const b = List.of(4, 5, 6); assert.notEqual(a, b); ``` ### .notReferenceEqual(actual, expected) - **@param** _{Collection}_ actual - **@param** _{Collection}_ expected Asserts that the reference of `actual` is not equivalent to the reference of `expected`. This method preserves the original behavior of Chai's `notEqual`. See [issue #210](https://github.com/astorije/chai-immutable/issues/210) for more details. ```js const a = List.of(1, 2, 3); const b = a; const c = List.of(1, 2, 3); assert.throws(() => assert.notReferenceEqual(a, b)); assert.notReferenceEqual(a, c); ``` ### .sizeOf(collection, length) - **@param** _{ Collection }_ collection - **@param** _{ Number }_ size Asserts that the immutable collection has the expected size. ```js assert.sizeOf(List.of(1, 2, 3), 3); assert.sizeOf(new List(), 0); ``` ================================================ FILE: appveyor.yml ================================================ # http://www.appveyor.com/docs/appveyor-yml # Build version format version: '{build}' # Do not build on tags (GitHub only) skip_tags: true environment: matrix: - nodejs_version: 12 # EOL: 2022-04-01 - nodejs_version: 10 # EOL: 2021-04-01 install: - ps: Install-Product node $env:nodejs_version - yarn install --frozen-lockfile - yarn add --dev mocha-appveyor-reporter - echo --reporter mocha-appveyor-reporter >> test/mocha.opts test_script: - node --version - yarn --version - yarn test # cache npm modules cache: - "%LOCALAPPDATA%\\Yarn" # Don't actually build build: off ================================================ FILE: chai-immutable.js ================================================ 'use strict'; ((context, factory) => { if ( typeof require === 'function' && typeof exports === 'object' && typeof module === 'object' ) { // Node.js module.exports = factory(require('immutable')); } else { // Other environments (usually ================================================ FILE: test/test.js ================================================ 'use strict'; // From http://stackoverflow.com/a/728694 function clone(obj) { if (obj === null || typeof obj !== 'object') { return obj; } const copy = obj.constructor(); for (const attr in obj) { if (Object.prototype.hasOwnProperty.call(obj, attr)) { copy[attr] = obj[attr]; } } return copy; } // These can be global if they are coming from a browser environment, so `let` // cannot be used here. var chai; // eslint-disable-line no-var var chaiImmutable; // eslint-disable-line no-var var Immutable; // eslint-disable-line no-var if (!chai) { chai = require('chai'); chaiImmutable = require('../chai-immutable'); Immutable = require('immutable'); chai.use(chaiImmutable); } const clonedImmutable = clone(Immutable); const { assert, expect } = chai; const { List, Map, Set, Stack } = Immutable; /** * Test helper to check that a given function (wrapping the assertion) will * fail. */ function fail(fn, msg) { if (msg) { expect(fn).to.throw(chai.AssertionError, msg); } else { expect(fn).to.throw(chai.AssertionError); } } describe('chai-immutable', function () { const list3 = List.of(1, 2, 3); const deepMap = new Map({ foo: 'bar', list: List.of(1, 2, 3) }); const sameDeepMap = new Map({ foo: 'bar', list: List.of(1, 2, 3) }); const differentDeepMap = new Map({ foo: 'bar', list: List.of(42) }); const clonedImmutableList = clonedImmutable.List.of(1, 2, 3); describe('BDD interface', function () { describe('empty property', function () { it('should pass given an empty collection', function () { expect(new List()).to.be.empty; }); it('should pass using `not` given a non-empty collection', function () { expect(list3).to.not.be.empty; }); it('should not affect the original assertions', function () { expect([]).to.be.empty; expect('').to.be.empty; expect({}).to.be.empty; }); it('should fail given a non-empty collection', function () { fail(() => expect(list3).to.be.empty); }); it('should fail using `not` given an empty collection', function () { fail(() => expect(new List()).to.not.be.empty); }); it('should work if using different copies of Immutable', function () { expect(new clonedImmutable.List()).to.be.empty; }); }); describe('equal method', function () { it('should pass given equal values', function () { expect(list3).to.equal(List.of(1, 2, 3)); }); it('should pass using `not` given different values', function () { expect(list3).to.not.equal(new List()); }); it('aliases of equal should also work', function () { expect(list3).to.equal(List.of(1, 2, 3)); expect(list3).to.not.equal(new List()); expect(list3).to.equals(List.of(1, 2, 3)); expect(list3).to.not.equals(new List()); expect(list3).to.eq(List.of(1, 2, 3)); expect(list3).to.not.eq(new List()); expect(list3).to.eql(List.of(1, 2, 3)); expect(list3).to.eqls(List.of(1, 2, 3)); expect(list3).to.not.eql(new List()); expect(list3).to.not.eqls(new List()); expect(list3).to.deep.equal(List.of(1, 2, 3)); expect(list3).to.not.deep.equal(new List()); }); it('should not affect the original assertions', function () { expect('hello').to.equal('hello'); expect(42).to.equal(42); expect(1).to.not.equal(true); expect({ foo: 'bar' }).to.not.equal({ foo: 'bar' }); expect({ foo: 'bar' }).to.deep.equal({ foo: 'bar' }); }); it('should pass given deeply equal values', function () { expect(deepMap).to.equal(sameDeepMap); expect(deepMap).to.equals(sameDeepMap); expect(deepMap).to.eq(sameDeepMap); expect(deepMap).to.eql(sameDeepMap); expect(deepMap).to.eqls(sameDeepMap); expect(deepMap).to.deep.equal(sameDeepMap); }); it('should pass using `not` given deeply different values', function () { expect(deepMap).to.not.equal(differentDeepMap); expect(deepMap).to.not.equals(differentDeepMap); expect(deepMap).to.not.eq(differentDeepMap); expect(deepMap).to.not.eql(differentDeepMap); expect(deepMap).to.not.eqls(differentDeepMap); expect(deepMap).to.not.deep.equal(differentDeepMap); }); it('should pass using `not` given a non-Immutable value', function () { expect([]).to.not.equal(new List()); }); // See https://github.com/astorije/chai-immutable/issues/7 it('should display a helpful failure output on big objects', function () { const actual = new Map({ foo: 'foo foo foo foo foo foo foo foo' }); const expected = new Map({ bar: 'bar bar bar bar bar bar bar bar' }); // AssertionError: expected { Object (foo) } to equal { Object (bar) } // + expected - actual // // { // - "foo": "foo foo foo foo foo foo foo foo" // + "bar": "bar bar bar bar bar bar bar bar" // } fail( () => expect(actual).to.equal(expected), 'expected { Object (foo) } to equal { Object (bar) }' ); }); it('should fail given a non-Immutable value', function () { fail(() => expect([]).to.equal(new List())); }); it('should fail given different values', function () { fail(() => expect(list3).to.equal(new List())); }); it('should fail using `not` given equal values', function () { fail(() => expect(list3).to.not.equal(List.of(1, 2, 3))); }); it('should fail given deeply different values', function () { fail(() => expect(deepMap).to.equal(differentDeepMap)); fail(() => expect(deepMap).to.equals(differentDeepMap)); fail(() => expect(deepMap).to.eq(differentDeepMap)); fail(() => expect(deepMap).to.eql(differentDeepMap)); fail(() => expect(deepMap).to.eqls(differentDeepMap)); fail(() => expect(deepMap).to.deep.equal(differentDeepMap)); }); it('should fail using `not` given deeply equal values', function () { fail(() => expect(deepMap).to.not.equal(sameDeepMap)); fail(() => expect(deepMap).to.not.equals(sameDeepMap)); fail(() => expect(deepMap).to.not.eq(sameDeepMap)); fail(() => expect(deepMap).to.not.eql(sameDeepMap)); fail(() => expect(deepMap).to.not.eqls(sameDeepMap)); fail(() => expect(deepMap).to.not.deep.equal(sameDeepMap)); }); it('should work if using different copies of Immutable', function () { expect(clonedImmutableList).to.equal(List.of(1, 2, 3)); }); }); describe('referenceEqual method', function () { it('should pass for equal references', function () { const list1 = List.of(1, 2, 3); const list2 = list1; expect(list1).to.referenceEqual(list2); }); it('should not pass for different immutable collections with equal values', function () { const list1 = List.of(1, 2, 3); const list2 = List.of(1, 2, 3); expect(list1).to.not.referenceEqual(list2); }); }); describe('include method', function () { it('should pass given an existing value', function () { expect(new List([1, 2, 3])).to.include(2); expect(new List([1, 2, 3])).to.deep.include(2); }); it('should pass using `not` given an inexisting value', function () { expect(new List([1, 2, 3])).to.not.include(42); expect(new List([1, 2, 3])).to.not.deep.include(42); }); it('should chain and pass given an existing key', function () { expect(new Map({ foo: 'bar', hello: 'world' })).to.include.keys('foo'); expect(new Map({ foo: 'bar', hello: 'world' })).to.deep.include.keys( 'foo' ); }); it('should chain and pass using `not` given an inexisting key', function () { expect(new Map({ foo: 'bar', hello: 'world' })).to.not.include.keys( 'not-foo' ); expect( new Map({ foo: 'bar', hello: 'world' }) ).to.not.deep.include.keys('not-foo'); }); it('aliases of include should also work', function () { expect(new List([1, 2, 3])).contain(2); expect(new List([1, 2, 3])).not.contain(42); expect(new List([1, 2, 3])).contains(2); expect(new List([1, 2, 3])).not.contains(42); expect(new List([1, 2, 3])).includes(2); expect(new List([1, 2, 3])).not.includes(42); }); it('should not affect the original assertions', function () { expect([1, 2, 3]).to.include(2); expect([1, 2, 3]).to.deep.include(2); expect('foobar').to.contain('foo'); expect('foobar').to.deep.contain('foo'); expect({ foo: 'bar', hello: 'world' }).to.include.keys('foo'); expect({ foo: 'bar', hello: 'world' }).to.deep.include.keys('foo'); }); // See https://github.com/astorije/chai-immutable/issues/7 it('should display a helpful failure output on big objects', function () { const lengthyMap = new Map({ foo: 'foo foo foo foo foo foo foo foo ' }); fail(() => expect(lengthyMap).to.include('not-foo'), /(foo ){8}/); fail(() => expect(lengthyMap).to.deep.include('not-foo'), /(foo ){8}/); }); it('should fail given an inexisting value', function () { fail(() => expect(new List([1, 2, 3])).to.include(42)); fail(() => expect(new List([1, 2, 3])).to.deep.include(42)); }); it('should fail using `not` given an existing value', function () { fail(() => expect(new List([1, 2, 3])).not.to.include(2)); fail(() => expect(new List([1, 2, 3])).not.to.deep.include(2)); }); it('should chain and fail given an inexisting key', function () { fail(() => expect(new Map({ foo: 'bar', hello: 'world' })).to.include.keys( 'not-foo' ) ); fail(() => expect(new Map({ foo: 'bar', hello: 'world' })).to.deep.include.keys( 'not-foo' ) ); }); it('should chain and fail using `not` given an existing key', function () { fail(() => expect(new Map({ foo: 'bar', hello: 'world' })).to.not.include.keys( 'foo' ) ); fail(() => expect( new Map({ foo: 'bar', hello: 'world' }) ).to.not.deep.include.keys('foo') ); }); }); describe('keys method', function () { const obj = { x: 1, y: 2 }; const map = new Map(obj); it('should pass given an existing key', function () { expect(new Map({ x: 1 })).to.have.key('x'); expect(new Map({ x: 1 })).to.have.deep.key('x'); }); it('should pass using `not` given an inexisting key', function () { expect(map).to.not.have.key('z'); expect(map).to.not.have.deep.key('z'); }); it('should pass given multiple existing keys', function () { expect(map).to.have.keys('x', 'y'); expect(map).to.have.deep.keys('x', 'y'); }); it('should pass using `not` given multiple inexisting keys', function () { expect(map).to.not.have.keys('z1', 'z2'); expect(map).to.not.have.deep.keys('z1', 'z2'); }); it('should accept an Array of keys to check against', function () { expect(map).to.have.keys(['x', 'y']); expect(map).to.have.deep.keys(['x', 'y']); }); it('should accept a List of keys to check against', function () { expect(map).to.have.keys(new List(['x', 'y'])); expect(map).to.have.deep.keys(new List(['x', 'y'])); }); it('should accept a Set of keys to check against', function () { expect(map).to.have.keys(new Set(['x', 'y'])); expect(map).to.have.deep.keys(new Set(['x', 'y'])); }); it('should accept a Stack of keys to check against', function () { expect(map).to.have.keys(new Stack(['x', 'y'])); expect(map).to.have.deep.keys(new Stack(['x', 'y'])); }); it('should accept an Object to check against', function () { expect(map).to.have.keys({ x: 6, y: 7 }); expect(map).to.have.deep.keys({ x: 6, y: 7 }); expect(new List(['x', 'y'])).to.have.all.keys({ 0: 4, 1: 5 }); }); it('should accept a Map to check against', function () { expect(map).to.have.keys(new Map({ x: 6, y: 7 })); expect(map).to.have.deep.keys(new Map({ x: 6, y: 7 })); }); it('should error when given multiple non-scalar arguments', function () { const msg = 'when testing keys against an immutable collection, ' + 'you must give a single Array|Object|String|Collection argument or ' + 'multiple String arguments'; fail(() => expect(map).to.have.all.keys(['x'], 'y'), msg); fail(() => expect(map).to.have.all.keys(new List(['x']), 'y'), msg); fail(() => expect(map).to.have.all.keys(new Set(['x']), 'y'), msg); fail(() => expect(map).to.have.all.keys(new Stack(['x']), 'y'), msg); fail(() => expect(map).to.have.all.keys({ x: 1 }, 'y'), msg); fail(() => expect(map).to.have.all.keys(new Map({ x: 1 }), 'y'), msg); }); it('should error when given no arguments', function () { const msg = 'keys required'; fail(() => expect(map).to.have.all.keys([]), msg); fail(() => expect(map).to.have.all.keys(new List()), msg); fail(() => expect(map).to.have.all.keys(new Set()), msg); fail(() => expect(map).to.have.all.keys(new Stack()), msg); fail(() => expect(map).to.have.all.keys({}), msg); fail(() => expect(map).to.have.all.keys(new Map()), msg); }); it('should pass using `any` given an existing key', function () { expect(map).to.have.any.keys('x', 'z'); expect(map).to.have.any.deep.keys('x', 'z'); }); it('should pass using `not` and `any` given inexisting keys', function () { expect(map).to.not.have.any.keys('z1', 'z2'); expect(map).to.not.have.any.deep.keys('z1', 'z2'); }); it('should pass using `all` given existing keys', function () { expect(map).to.have.all.keys('x', 'y'); expect(map).to.have.all.deep.keys('x', 'y'); }); it('should pass using `not` and `all` given inexisting keys', function () { expect(map).to.not.have.all.keys('z1', 'y'); expect(map).to.not.have.all.deep.keys('z1', 'y'); }); it('should pass using `contain` given an existing key', function () { expect(map).to.contain.key('x'); expect(map).to.contain.deep.key('x'); }); it('should not affect the original assertions', function () { expect({ x: 1, y: 2 }).to.have.any.keys('x', 'z'); expect({ x: 1, y: 2 }).to.have.any.deep.keys('x', 'z'); expect({ x: 1, y: 2 }).to.have.any.keys('x'); expect({ x: 1, y: 2 }).to.have.any.deep.keys('x'); expect({ x: 1, y: 2 }).to.contain.any.keys('y', 'z'); expect({ x: 1, y: 2 }).to.contain.any.deep.keys('y', 'z'); expect({ x: 1, y: 2 }).to.contain.any.keys(['x']); expect({ x: 1, y: 2 }).to.contain.any.deep.keys(['x']); expect({ x: 1, y: 2 }).to.contain.any.keys({ x: 6 }); expect({ x: 1, y: 2 }).to.contain.any.deep.keys({ x: 6 }); expect({ x: 1, y: 2 }).to.have.all.keys(['x', 'y']); expect({ x: 1, y: 2 }).to.have.all.deep.keys(['x', 'y']); expect({ x: 1, y: 2 }).to.have.all.keys({ x: 6, y: 7 }); expect({ x: 1, y: 2 }).to.have.all.deep.keys({ x: 6, y: 7 }); expect({ x: 1, y: 2, z: 3 }).to.contain.all.keys(['x', 'y']); expect({ x: 1, y: 2, z: 3 }).to.contain.all.deep.keys(['x', 'y']); expect({ x: 1, y: 2, z: 3 }).to.contain.all.keys({ x: 6 }); expect({ x: 1, y: 2, z: 3 }).to.contain.all.deep.keys({ x: 6 }); }); // See https://github.com/astorije/chai-immutable/issues/7 it('should display a helpful failure output on big objects', function () { const lengthyMap = new Map({ foo: 'foo foo foo foo foo foo foo foo ' }); fail(() => expect(lengthyMap).to.have.keys('not-foo'), /(foo ){8}/); fail( () => expect(lengthyMap).to.have.deep.keys('not-foo'), /(foo ){8}/ ); }); it('should pass against Lists', function () { expect(new List(['x', 'y'])).to.have.all.keys(0, 1); expect(new List(['x', 'y'])).to.have.all.keys([0, 1]); }); it('should fail given an inexisting key', function () { fail(() => expect(new Map({ x: 1 })).to.have.key('z')); fail(() => expect(new Map({ x: 1 })).to.have.deep.key('z')); fail(() => expect({ x: 1 }).to.have.key('z')); fail(() => expect({ x: 1 }).to.have.deep.key('z')); }); it('should fail given multiple inexisting keys', function () { fail(() => expect(map).to.have.keys('z1', 'z2')); fail(() => expect(map).to.have.deep.keys('z1', 'z2')); }); it('should fail using `not` given multiple existing keys', function () { fail(() => expect(map).to.not.have.keys('x', 'y')); fail(() => expect(map).to.not.have.deep.keys('x', 'y')); }); it('should fail using `any` given inexisting keys', function () { fail(() => expect(map).to.have.any.keys('z1', 'z2')); fail(() => expect(map).to.have.any.deep.keys('z1', 'z2')); }); it('should fail using `not` and `any` given an existing key', function () { fail(() => expect(map).to.not.have.any.keys('x', 'z')); fail(() => expect(map).to.not.have.any.deep.keys('x', 'z')); }); it('should fail using `all` given an inexisting key', function () { fail(() => expect(map).to.have.all.keys('z1', 'y')); fail(() => expect(map).to.have.all.deep.keys('z1', 'y')); }); it('should fail using `not` and `all` given existing keys', function () { fail(() => expect(map).to.not.have.all.keys('x', 'y')); fail(() => expect(map).to.not.have.all.deep.keys('x', 'y')); }); it('should fail using `contain` given an inexisting key', function () { fail(() => expect(map).to.contain.key('z')); fail(() => expect(map).to.contain.deep.key('z')); }); it('should work if using different copies of Immutable', function () { expect(new clonedImmutable.Map({ x: 1 })).to.have.key('x'); expect(new clonedImmutable.Map({ x: 1 })).to.have.deep.key('x'); }); }); describe('property property', function () { it('should not affect the original assertion', function () { expect({ x: 1 }).to.have.property('x', 1); }); it('should not affect the original assertion using `not`', function () { expect({ x: 1 }).not.to.have.property('z'); expect({ x: 1 }).not.to.have.property('z', 42); }); // All following tests assert against regular `.property` and // `.deep.property`. In the Immutable world, these are supposed to carry // the same meaning (similar to `.equal` vs. `.deep.equal`). const obj = Immutable.fromJS({ x: 1 }); const nestedObj = Immutable.fromJS({ x: 1, y: { x: 2, y: 3 } }); it('should fail given an inexisting property', function () { fail(() => expect(obj).to.have.property('z')); fail(() => expect(obj).to.have.deep.property('z')); }); it('should pass using `not` given an inexisting property', function () { expect(obj).not.to.have.property('z'); expect(obj).not.to.have.deep.property('z'); }); it('should pass using `not` given an inexisting property and value', function () { expect(obj).not.to.have.property('z', 42); expect(obj).not.to.have.deep.property('z', 42); }); it('should pass given an existing property', function () { expect(obj).to.have.property('x'); expect(obj).to.have.deep.property('x'); }); it('should fail using `not` given an existing property', function () { fail(() => expect(obj).not.to.have.property('x')); fail(() => expect(obj).not.to.have.deep.property('x')); }); it('should fail given a property with a bad value', function () { fail(() => expect(obj).to.have.property('x', 'different')); fail(() => expect(obj).to.have.deep.property('x', 'different')); }); it('should pass given a property with the good value', function () { expect(obj).to.have.property('x', 1); expect(obj).to.have.deep.property('x', 1); }); it('should pass given an immutable value', function () { const obj2 = Immutable.fromJS({ foo: { bar: 42 } }); expect(obj2).to.have.property('foo', new Map({ bar: 42 })); expect(obj2).to.have.deep.property('foo', new Map({ bar: 42 })); }); it('should change the subject to the value of that property', function () { const sub = nestedObj.get('y'); expect(nestedObj).to.have.property('y').that.equal(sub); expect(nestedObj).to.have.deep.property('y').that.equal(sub); }); describe('using the `nested` flag', function () { it('should not affect the original assertion', function () { expect({ x: 1, y: { x: 2, y: 3 } }).to.have.nested.property('y.x', 2); expect({ x: 1, y: { x: 2, y: 3 } }).to.have.nested.deep.property( 'y.x', 2 ); }); it('should not affect the original assertion using `not`', function () { expect({ x: 1, y: { x: 2 } }).not.to.have.nested.property('z.z'); expect({ x: 1, y: { x: 2 } }).not.to.have.nested.deep.property('z.z'); expect({ x: 1, y: { x: 2 } }).not.to.have.nested.property('z.z', 42); expect({ x: 1, y: { x: 2 } }).not.to.have.nested.deep.property( 'z.z', 42 ); }); it('should fail given an inexisting property', function () { fail(() => expect(nestedObj).to.have.nested.property(['y', 'z'])); fail(() => expect(nestedObj).to.have.nested.deep.property(['y', 'z']) ); }); it('should pass using `not` given an inexisting property', function () { expect(nestedObj).not.to.have.nested.property(['y', 'z']); expect(nestedObj).not.to.have.nested.deep.property(['y', 'z']); expect(nestedObj).not.to.have.property('a', new Map({ x: 2 })); }); it('should pass using `not` given an inexisting property and value', function () { expect(obj).not.to.have.nested.property(['y', 'x'], 'different'); expect(obj).not.to.have.nested.deep.property(['y', 'x'], 'different'); }); it('should pass given an existing property', function () { expect(nestedObj).to.have.nested.property(['y', 'x']); expect(nestedObj).to.have.nested.deep.property(['y', 'x']); }); it('should pass given an index', function () { const obj2 = Immutable.fromJS({ items: ['a', 'b', 'c'] }); expect(obj2).to.have.nested.property(['items', 2], 'c'); expect(obj2).to.have.nested.deep.property(['items', 2], 'c'); }); it('should fail using `not` given an existing property', function () { fail(() => expect(nestedObj).not.to.have.nested.property(['y', 'x'])); fail(() => expect(nestedObj).not.to.have.nested.deep.property(['y', 'x']) ); }); it('should fail given a property with a bad value', function () { fail(() => expect(nestedObj).to.have.nested.property(['y', 'x'], 'different') ); fail(() => expect(nestedObj).to.have.nested.deep.property( ['y', 'x'], 'different' ) ); }); it('should pass given a property with the good value', function () { expect(nestedObj).to.have.nested.property(['y', 'x'], 2); expect(nestedObj).to.have.nested.deep.property(['y', 'x'], 2); }); it('should fail using `not` given a property with good value', function () { fail(() => expect(nestedObj).not.to.have.nested.property(['y', 'x'], 2) ); fail(() => expect(nestedObj).not.to.have.nested.deep.property(['y', 'x'], 2) ); }); it('should pass using `not` given a property with a bad value', function () { expect(nestedObj).not.to.have.nested.property( ['y', 'x'], 'different' ); expect(nestedObj).not.to.have.nested.deep.property( ['y', 'x'], 'different' ); }); it('should pass given an immutable value', function () { const nestedObj2 = Immutable.fromJS({ foo: [{ bar: 42 }] }); expect(nestedObj2).to.have.nested.property( 'foo[0]', new Map({ bar: 42 }) ); expect(nestedObj2).to.have.nested.deep.property( 'foo[0]', new Map({ bar: 42 }) ); }); }); describe('given a string-based path', function () { const nestedObj2 = Immutable.fromJS({ items: [{ name: 'Jane' }, { name: 'John' }, { name: 'Jim' }], }); it('should pass using `nested` given a single index', function () { expect(nestedObj2.get('items')) .to.have.nested.property('[1]') .that.equals(new Map({ name: 'John' })); expect(nestedObj2.get('items')) .to.have.nested.deep.property('[1]') .that.equals(new Map({ name: 'John' })); }); it('should pass using `nested` given a single key', function () { expect(nestedObj2) .to.have.nested.property('items') .that.equals( new List([ new Map({ name: 'Jane' }), new Map({ name: 'John' }), new Map({ name: 'Jim' }), ]) ); expect(nestedObj2) .to.have.nested.deep.property('items') .that.equals( new List([ new Map({ name: 'Jane' }), new Map({ name: 'John' }), new Map({ name: 'Jim' }), ]) ); }); it('should pass using `nested` starting with an index', function () { expect(nestedObj2.get('items')).to.have.nested.property( '[0].name', 'Jane' ); expect(nestedObj2.get('items')).to.have.nested.deep.property( '[0].name', 'Jane' ); }); it('should pass using `nested` ending with an index', function () { expect(nestedObj2) .to.have.nested.property('items[1]') .that.equals(new Map({ name: 'John' })); expect(nestedObj2) .to.have.nested.deep.property('items[1]') .that.equals(new Map({ name: 'John' })); }); it('should pass using `nested` given mix of keys and indices', function () { expect(nestedObj2).to.have.nested.property('items[2].name', 'Jim'); expect(nestedObj2).to.have.nested.deep.property( 'items[2].name', 'Jim' ); }); it('should expect unescaped path strings', function () { const css = new Map({ '.link[target]': 42 }); expect(css).to.have.property('.link[target]', 42); expect(css).to.have.deep.property('.link[target]', 42); }); it('should expect escaped path strings using `nested`', function () { const nestedCss = new Map({ '.link': new Map({ '[target]': 42 }) }); expect(nestedCss).to.have.nested.property('\\.link.\\[target\\]', 42); expect(nestedCss).to.have.nested.deep.property( '\\.link.\\[target\\]', 42 ); }); }); }); describe('size method', function () { it('should pass given the right size', function () { expect(list3).to.have.size(3); }); it('should pass using `not` given the wrong size', function () { expect(list3).to.not.have.size(42); }); it('should also work with alias sizeOf', function () { expect(list3).to.have.sizeOf(3); expect(list3).to.not.have.sizeOf(42); }); it('should fail given the wrong size', function () { fail(() => expect(list3).to.have.size(42)); }); it('should fail using `not` given the right size', function () { fail(() => expect(list3).to.not.have.size(3)); }); it('should work if using different copies of Immutable', function () { expect(clonedImmutableList).to.have.size(3); }); }); describe('size property', function () { it('above should pass given a good min size', function () { expect(list3).to.have.size.above(2); }); it('above should pass using `not` given a bad min size', function () { expect(list3).to.not.have.size.above(42); }); it('aliases of above should also work', function () { expect(list3).to.have.size.gt(2); expect(list3).to.have.size.greaterThan(2); expect(list3).to.not.have.size.gt(42); expect(list3).to.not.have.size.greaterThan(42); }); it('should not affect the original assertions of above', function () { expect('foo').to.have.length.above(2); expect([1, 2, 3]).to.have.length.above(2); }); it('above should fail given a bad min size', function () { fail(() => expect(list3).to.have.size.above(42)); }); it('above should fail using `not` given a good min size', function () { fail(() => expect(list3).to.not.have.size.above(2)); }); it('below should pass given a good max size', function () { expect(list3).to.have.size.below(42); }); it('below should pass using `not` given a bad max size', function () { expect(list3).to.not.have.size.below(1); }); it('aliases of below should also work', function () { expect(list3).to.have.size.lt(4); expect(list3).to.have.size.lessThan(4); expect(list3).to.not.have.size.lt(1); expect(list3).to.not.have.size.lessThan(1); }); it('should not affect the original assertions of below', function () { expect('foo').to.have.length.below(4); expect([1, 2, 3]).to.have.length.below(4); }); it('below should fail given a bad max size', function () { fail(() => expect(list3).to.have.size.below(1)); }); it('below should fail using `not` given a good max size', function () { fail(() => expect(list3).to.not.have.size.below(42)); }); it('within should pass given a good range', function () { expect(list3).to.have.size.within(2, 42); }); it('within should pass using `not` given a bad range', function () { expect(list3).to.not.have.size.within(10, 20); }); it('should not affect the original assertions of within', function () { expect('foo').to.have.length.within(2, 4); expect([1, 2, 3]).to.have.length.within(2, 4); }); it('within should fail given a bad range', function () { fail(() => expect(list3).to.have.size.within(10, 20)); }); it('within should fail using `not` given a good range', function () { fail(() => expect(list3).to.not.have.size.within(2, 42)); }); it('least should pass given a good min size', function () { expect(list3).to.have.size.of.at.least(2); }); it('least should pass using `not` given a bad min size', function () { expect(list3).to.not.have.size.of.at.least(42); }); it('aliases of least should also work', function () { expect(list3).to.have.size.gte(2); expect(list3).to.not.have.size.gte(42); }); it('should not affect the original assertions of least', function () { expect('foo').to.have.length.of.at.least(2); expect([1, 2, 3]).to.have.length.of.at.least(3); }); it('least should fail given a bad min size', function () { fail(() => expect(list3).to.have.size.of.at.least(42)); }); it('least should fail using `not` given a good min size', function () { fail(() => expect(list3).to.not.have.size.of.at.least(2)); }); it('most should pass given a good max size', function () { expect(list3).to.have.size.of.at.most(42); }); it('most should pass using `not` given a bad max size', function () { expect(list3).to.not.have.size.of.at.most(2); }); it('aliases of most should also work', function () { expect(list3).to.have.size.lte(42); expect(list3).to.not.have.size.lte(2); }); it('should not affect the original assertions of most', function () { expect('foo').to.have.length.of.at.most(4); expect([1, 2, 3]).to.have.length.of.at.most(3); }); it('most should fail given a good max size', function () { fail(() => expect(list3).to.have.size.of.at.most(2)); }); it('most should fail using `not` given a bad max size', function () { fail(() => expect(list3).to.not.have.size.of.at.most(42)); }); it('should work if using different copies of Immutable', function () { expect(clonedImmutableList).to.have.size.above(2); }); }); }); describe('TDD interface', function () { describe('equal assertion', function () { it('should pass given equal values', function () { assert.equal(list3, List.of(1, 2, 3)); }); it('should pass given deeply equal values', function () { assert.equal(deepMap, sameDeepMap); }); it('should not affect the original assertion', function () { assert.equal(42, 42); assert.equal(3, '3'); }); // See https://github.com/astorije/chai-immutable/issues/7 it('should display a helpful failure output on big objects', function () { const actual = new Map({ foo: 'foo foo foo foo foo foo foo foo ' }); const expected = new Map({ bar: 'bar bar bar bar bar bar bar bar ' }); // AssertionError: expected { Object (foo) } to equal { Object (bar) } // + expected - actual // // { // - "foo": "foo foo foo foo foo foo foo foo" // + "bar": "bar bar bar bar bar bar bar bar" // } fail( () => assert.equal(actual, expected), 'expected { Object (foo) } to equal { Object (bar) }' ); }); it('should fail given a non-Immutable value', function () { fail(() => assert.equal([], new List())); }); it('should fail given different values', function () { fail(() => assert.equal(list3, new List())); }); it('should fail given deeply different values', function () { fail(() => assert.equal(deepMap, differentDeepMap)); }); it('should work if using different copies of Immutable', function () { assert.equal(clonedImmutableList, List.of(1, 2, 3)); }); }); describe('referenceEqual assertion', function () { it('should pass for equal references', function () { const list1 = List.of(1, 2, 3); const list2 = list1; assert.referenceEqual(list1, list2); }); it('should not pass for different immutable collections with equal values', function () { const list1 = List.of(1, 2, 3); const list2 = List.of(1, 2, 3); fail(() => assert.referenceEqual(list1, list2)); }); }); describe('notEqual assertion', function () { it('should pass given different values', function () { assert.notEqual(list3, new List()); }); it('should pass given deeply different values', function () { assert.notEqual(deepMap, differentDeepMap); }); it('should not affect the original assertion', function () { assert.notEqual('oui', 'non'); assert.notEqual({ foo: 'bar' }, { foo: 'bar' }); }); it('should pass given a non-Immutable value', function () { assert.notEqual([], new List()); }); it('should fail given equal values', function () { fail(() => assert.notEqual(list3, List.of(1, 2, 3))); }); it('should fail given deeply equal values', function () { fail(() => assert.notEqual(deepMap, sameDeepMap)); }); it('should work if using different copies of Immutable', function () { assert.notEqual(clonedImmutableList, List.of()); }); }); describe('notReferenceEqual assertion', function () { it('should pass for different immutable collections with equal values', function () { const list1 = List.of(1, 2, 3); const list2 = List.of(1, 2, 3); assert.notReferenceEqual(list1, list2); }); it('should not pass for different immutable collections with equal values', function () { const list1 = List.of(1, 2, 3); const list2 = list1; fail(() => assert.notReferenceEqual(list1, list2)); }); }); describe('unoverridden strictEqual and deepEqual assertions', function () { it('should pass given equal values', function () { assert.strictEqual(list3, List.of(1, 2, 3)); assert.deepEqual(list3, List.of(1, 2, 3)); }); it('should pass given deeply equal values', function () { assert.strictEqual(deepMap, sameDeepMap); assert.deepEqual(deepMap, sameDeepMap); }); it('should fail given different values', function () { fail(() => assert.strictEqual(list3, new List())); fail(() => assert.deepEqual(list3, new List())); }); it('should fail given deeply different values', function () { fail(() => assert.strictEqual(deepMap, differentDeepMap)); fail(() => assert.deepEqual(deepMap, differentDeepMap)); }); it('should work if using different copies of Immutable', function () { assert.strictEqual(clonedImmutableList, List.of(1, 2, 3)); assert.deepEqual(clonedImmutableList, List.of(1, 2, 3)); }); }); describe('unoverridden notStrictEqual and notDeepEqual assertions', function () { it('should pass given different values', function () { assert.notStrictEqual(list3, new List()); assert.notDeepEqual(list3, new List()); }); it('should pass given deeply different values', function () { assert.notStrictEqual(deepMap, differentDeepMap); assert.notDeepEqual(deepMap, differentDeepMap); }); it('should fail given equal values', function () { fail(() => assert.notStrictEqual(list3, List.of(1, 2, 3))); fail(() => assert.notDeepEqual(list3, List.of(1, 2, 3))); }); it('should fail given deeply equal values', function () { fail(() => assert.notStrictEqual(deepMap, sameDeepMap)); fail(() => assert.notDeepEqual(deepMap, sameDeepMap)); }); it('should work if using different copies of Immutable', function () { assert.notStrictEqual(clonedImmutableList, new List()); assert.notDeepEqual(clonedImmutableList, new List()); }); }); describe('include assertion', function () { const map1 = new Map({ a: 1 }); const map2 = new Map({ b: 2 }); const list = new List([map1, map2]); const map = new Map({ foo: map1, bar: map2 }); it('should ensure deep equality', function () { assert.include(list, map1); assert.include(list, new Map({ a: 1 })); assert.include(map, map1); assert.include(map, new Map({ a: 1 })); }); it('should find plain values', function () { assert.include(map1, 1); }); it('should treat partial collections as sub-collections', function () { assert.include(map, new Map({ foo: map1 })); assert.include(map, new Map({ foo: map1, bar: map2 })); }); }); describe('notInclude assertion', function () { const map1 = new Map({ a: 1 }); const map2 = new Map({ b: 2 }); const list = new List([map1, map2]); const map = new Map({ foo: map1, bar: map2 }); it('should ensure deep equality', function () { fail(() => assert.notInclude(map, new Map({ foo: map1 }))); fail(() => assert.notInclude(map, new Map({ foo: map1, bar: map2 }))); }); it('should fail for existing collections', function () { fail(() => assert.notInclude(list, map1)); fail(() => assert.notInclude(list, new Map({ a: 1 }))); }); it('should treat partial overlap as failure', function () { assert.notInclude(map, new Map({ foo: new Map({ a: 1, b: 2 }) })); }); }); describe('property assertions', function () { const obj = Immutable.fromJS({ x: 1 }); const nestedObj = Immutable.fromJS({ x: 1, y: { x: 2, y: 3 } }); it('should pass for existing property', function () { assert.property(obj, 'x'); }); it('should fail for missing property', function () { fail(() => assert.property(obj, 'z')); }); it('should pass for missing property using `not`', function () { assert.notProperty(obj, 'z'); }); it('should fail for existing property using `not`', function () { fail(() => assert.notProperty(obj, 'x')); }); it('should pass for existing property and value', function () { assert.propertyVal(obj, 'x', 1); assert.deepPropertyVal(obj, 'x', 1); }); it('should fail for wrong property or value', function () { fail(() => assert.propertyVal(obj, 'z', 1)); fail(() => assert.deepPropertyVal(obj, 'z', 1)); fail(() => assert.propertyVal(obj, 'x', 42)); fail(() => assert.deepPropertyVal(obj, 'x', 42)); }); it('should pass for wrong property or value using `not`', function () { assert.notPropertyVal(obj, 'z', 1); assert.notDeepPropertyVal(obj, 'z', 1); assert.notPropertyVal(obj, 'x', 42); assert.notDeepPropertyVal(obj, 'x', 42); assert.notPropertyVal(obj, 'foo', new Map({ bar: 'baz' })); assert.notDeepPropertyVal(obj, 'foo', new Map({ bar: 'baz' })); }); it('should fail for existing property and value using `not`', function () { fail(() => assert.notPropertyVal(obj, 'x', 1)); fail(() => assert.notDeepPropertyVal(obj, 'x', 1)); }); it('should succeed for equal nested property', function () { assert.nestedProperty(nestedObj, ['y', 'x']); }); it('should fail for unequal nested property', function () { fail(() => assert.nestedPropertyVal(nestedObj, ['y', 'x'], 42)); fail(() => assert.deepNestedPropertyVal(nestedObj, ['y', 'x'], 42)); }); }); describe('sizeOf assertion', function () { it('should pass given the right size', function () { assert.sizeOf(list3, 3); }); it('should work with empty collections', function () { assert.sizeOf(new List(), 0); }); it('should fail given the wrong size', function () { fail(() => assert.sizeOf(list3, 42)); }); it('should work if using different copies of Immutable', function () { assert.sizeOf(clonedImmutableList, 3); }); }); }); });