Immutable.fromJS recursively converts native JavaScript objects and arrays into Immutable.js collections. Nested maps and lists become immutable too, giving persistent-data-structure code a consistent representation. section 9.3.2
Listing 9.9 Conversion to immutable data
var libraryData = Immutable.fromJS({
"catalog": {
"booksByIsbn": {
"978-1779501127": {
"isbn": "978-1779501127",
"title": "Watchmen",
"publicationYear": 1987,
"authorIds": ["alan-moore",
"dave-gibbons"]
}
},
"authorsById": {
"alan-moore": {
"name": "Alan Moore",
"bookIsbns": ["978-1779501127"]
},
"dave-gibbons": {
"name": "Dave Gibbons",
"bookIsbns": ["978-1779501127"]
}
}
}
});
Convert a nested library catalog in one expression; catalog, books, authors, and identifier lists become immutable values. section 9.3.2
Use the converted value as the boundary between ordinary JavaScript data and the Immutable.js API:
- Read fields with Immutable.get or nested paths with Immutable.getIn rather than dot or bracket notation. section 9.3.2
- Prepare expected values and test fixtures with Immutable.fromJS, then compare collections with Immutable.isEqual. section 9.4.1 section 9.4.2
A practical boundary
A book-search test converts both catalog data and the expected result with Immutable.fromJS, then verifies a matching Watchmen result and an empty Batman result. section 9.4.1
