Skip to content

Commit

Permalink
Stable ORM Release v1.1.0: No more fixture mutation on restart.
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Feb 23, 2018
1 parent 6aa3cf2 commit c571077
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/mem-server-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,9 @@ function registerModels(modelFixtureTree) {

function resetDatabase(models, modelFixtureTree) {
return Object.keys(models).reduce((result, modelName) => {
result[modelName] = Array.from(modelFixtureTree[modelName].fixtures);
result[modelName] = Array.from(modelFixtureTree[modelName].fixtures, (fixtureObject) => {
return Object.assign({}, fixtureObject);
});

const modelPrimaryKey = result[modelName].reduce(([existingPrimaryKey, primaryKeys], model) => {
const primaryKey = getModelPrimaryKey(model, existingPrimaryKey, modelName);
Expand Down
4 changes: 3 additions & 1 deletion lib/mem-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ function registerModels(modelFixtureTree) {

function resetDatabase(models, modelFixtureTree) {
return Object.keys(models).reduce((result, modelName) => {
result[modelName] = Array.from(modelFixtureTree[modelName].fixtures);
result[modelName] = Array.from(modelFixtureTree[modelName].fixtures, (fixtureObject) => {
return Object.assign({}, fixtureObject);
});

const modelPrimaryKey = result[modelName].reduce(([existingPrimaryKey, primaryKeys], model) => {
const primaryKey = getModelPrimaryKey(model, existingPrimaryKey, modelName);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "memserver",
"version": "1.0.5",
"version": "1.1.0",
"description": "in-memory database/ORM and http mock server you can run in-browser and node environments. Built for large frontend teams, fast tests and rapid prototyping",
"main": "lib/index.js",
"license": "ISC",
Expand Down
56 changes: 53 additions & 3 deletions test/mem-server.start.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('MemServer start/stop functionality', function() {
assert.ok(MemServer.Server.shutdown.calledOnce, 'MemServer.shutdown() calls shutdown on Pretender instance');
});

it('can be shut down and started again with restarted state', () => {
it('can be shut down and started again with restarted INITIAL STATE, NO MUTATION', () => {
fs.mkdirSync(`./memserver/fixtures`);
fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photos.js`, `export default [
{
Expand Down Expand Up @@ -226,10 +226,60 @@ describe('MemServer start/stop functionality', function() {
assert.equal(Photo.findAll().length, 6);
assert.equal(PhotoComment.findAll().length, 5);

Photo.update({ id: 1, name: 'Mutated photo name' });
Photo.update({ id: 2, name: 'Another mutated photo name' });
PhotoComment.update({
uuid: '499ec646-493f-4eea-b92e-e383d94182f4', content: 'Mutated photo comment'
});

MemServer.shutdown();
MemServer.start();

assert.deepEqual(Photo.findAll(), initialPhotos);
assert.deepEqual(PhotoComment.findAll(), initialPhotoComments);
assert.deepEqual(Photo.findAll(), [
{
id: 1,
name: 'Ski trip',
href: 'ski-trip.jpeg',
is_public: false
},
{
id: 2,
name: 'Family photo',
href: 'family-photo.jpeg',
is_public: true
},
{
id: 3,
name: 'Selfie',
href: 'selfie.jpeg',
is_public: false
}
]);
assert.deepEqual(PhotoComment.findAll(), [
{
uuid: '499ec646-493f-4eea-b92e-e383d94182f4',
content: 'What a nice photo!',
photo_id: 1,
user_id: 1
},
{
uuid: '77653ad3-47e4-4ec2-b49f-57ea36a627e7',
content: 'I agree',
photo_id: 1,
user_id: 2
},
{
uuid: 'd351963d-e725-4092-a37c-1ca1823b57d3',
content: 'I was kidding',
photo_id: 1,
user_id: 1
},
{
uuid: '374c7f4a-85d6-429a-bf2a-0719525f5f29',
content: 'Interesting indeed',
photo_id: 2,
user_id: 1
}
]);
});
});

0 comments on commit c571077

Please sign in to comment.