Skip to content
This repository has been archived by the owner on Oct 8, 2019. It is now read-only.

Commit

Permalink
BREAKING(Profile): renamed completed partial methods, more tests
Browse files Browse the repository at this point in the history
Renamed addressComplete to identityComplete and infoComplete to
bankInfoComplete.
  • Loading branch information
Sjors committed Jun 26, 2017
1 parent 98f4a38 commit 7efd2c3
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"JasminePromiseMatchers",
"jasmine",
"spyOn",
"spyOnProperty",
"pending",
"fail"
]
Expand Down
18 changes: 8 additions & 10 deletions src/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Profile {
return Boolean(this._photos.address && this._photos.pancard && this.photos.photo);
}

get addressComplete () {
get identityComplete () {
return this.level > 1 || Boolean(
this.mobile &&
this.pancard &&
Expand All @@ -93,7 +93,7 @@ class Profile {
);
}

get infoComplete () {
get bankInfoComplete () {
return this.level > 1 || Boolean(
this.ifsc &&
this.bankAccountNumber
Expand All @@ -103,12 +103,8 @@ class Profile {
get complete () {
return this.level > 1 || Boolean(
this.photosComplete &&
this.address.complete &&
this.fullName &&
this.mobile &&
this.pancard &&
this.bankAccountNumber &&
this.ifsc
this.identityComplete &&
this.bankInfoComplete
);
}

Expand Down Expand Up @@ -214,7 +210,7 @@ class Profile {
assert(this.complete, 'Missing info, always check "complete" first');
assert(!this.readOnly, 'Profile is read-only');

return this._api.authPOST('api/v1/settings/uploaduserprofile', {
let payload = {
name: this.fullName,
mobile: this.mobile.replace(/\+91/g, '').replace(/ /g, ''),
pannumber: this.pancard,
Expand All @@ -227,7 +223,9 @@ class Profile {
pancard_photo: this.photos.pancard.base64,
photo: this.photos.photo.base64,
address_proof: this.photos.address.base64
}).then(res => {
};

return this._api.authPOST('api/v1/settings/uploaduserprofile', payload).then(res => {
if (res.status_code === 200) {
this._dirty = false;
this._address.didSave();
Expand Down
206 changes: 201 additions & 5 deletions tests/profile_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ describe('Profile', function () {
status_code: 200,
message: 'Unverified User',
user_status: 1
})
}),
authPOST: (method, payload) => {
if (payload.name !== 'Should Fail') {
return Promise.resolve({
status_code: 200,
user_status: 2
});
} else {
return Promise.resolve({
status_code: 714,
user_status: 2,
message: 'FAIL'
});
}
}
};

newUserObj = {
Expand Down Expand Up @@ -110,11 +124,193 @@ describe('Profile', function () {
});
});

describe('instance', function () {
// let newUserProfile;
describe('level 1', () => {
let profile;

beforeEach(function () {
// let newUserProfile = new Profile(newUserObj);
beforeEach(() => {
profile = new Profile(newUserObj, api);
});

it('should not be read-only', () => {
expect(profile.readOnly).toBeFalsy();
});

it('should not be dirty by default', () => {
expect(profile.dirty).toBeFalsy();
});

it('should be dirty if a field is changed', () => {
profile.fullName = 'John Do';
expect(profile.dirty).toBeTruthy();
});

it('should not be dirty if a field is not changed', () => {
profile.fullName = 'John Do';
profile.mobile = '1234567893';
profile.pancard = 'BAD876G570';
profile.bankAccountNumber = '123';
profile.ifsc = '456';
profile._dirty = false; // Pretend we saved

profile.fullName = 'John Do';
profile.mobile = '1234567893';
profile.pancard = 'BAD876G570';
profile.bankAccountNumber = '123';
profile.ifsc = '456';
expect(profile.dirty).toBeFalsy();
});

describe('photos complete', () => {
it('should check address, pancard and photo present', () => {
expect(profile.photosComplete).toBeFalsy();
profile._photos = {
address: 'base64',
pancard: 'base64',
photo: 'base64'
};
expect(profile.photosComplete).toBeTruthy();
});
});

describe('address complete', () => {
it('should check mobile, pancard full name and address details present', () => {
expect(profile.identityComplete).toBeFalsy();
profile.mobile = '123';
profile.pancard = '456';
profile.fullName = 'John Do';
profile._address = {complete: true};
expect(profile.identityComplete).toBeTruthy();
});
});

describe('info complete', () => {
it('should check IFSC and bank account', () => {
expect(profile.bankInfoComplete).toBeFalsy();
profile.ifsc = '123';
profile.bankAccountNumber = '456';
expect(profile.bankInfoComplete).toBeTruthy();
});
});

describe('complete', () => {
it('should check all the things', () => {
expect(profile.complete).toBeFalsy();

spyOnProperty(profile, 'photosComplete').and.returnValue(true);
spyOnProperty(profile, 'identityComplete').and.returnValue(true);
spyOnProperty(profile, 'bankInfoComplete').and.returnValue(true);

expect(profile.complete).toBeTruthy();
});
});

describe('verify()', () => {
beforeEach(() => {
profile.fullName = 'John Do';
profile.mobile = '+91234567893';
profile.pancard = 'BAD876G570';
profile.bankAccountNumber = '123';
profile.ifsc = '456';
profile._photos = {
address: {base64: 'base64'},
pancard: {base64: 'base64'},
photo: {base64: 'base64'}
};
profile._address = {
street: '1',
state: 'K',
city: 'Bangalore',
zipcode: '123',
country: 'IN',
complete: true,
didSave: () => {}
};

spyOn(api, 'authPOST').and.callThrough();
});

it('calls settings/uploaduserprofile', done => {
let checks = () => {
expect(api.authPOST).toHaveBeenCalled();
expect(api.authPOST.calls.argsFor(0)[0]).toEqual(('api/v1/settings/uploaduserprofile'));
};

profile.verify().then(checks).catch(fail).then(done);
});

it('should set readOnly to true', (done) => {
let checks = () => {
expect(profile.readOnly).toBeTruthy();
};

profile.verify().then(checks).catch(fail).then(done);
});

it('should set dirty to false', (done) => {
let checks = () => {
expect(profile.dirty).toBeFalsy();
};

profile.verify().then(checks).catch(fail).then(done);
});

describe('fails', () => {
beforeEach(() => {
profile.fullName = 'Should Fail';
});

it('should return the error message', (done) => {
let checks = (message) => {
expect(message).toEqual('FAIL');
};

profile.verify().then(fail).catch(checks).then(done);
});

it('should leave readOnly false', (done) => {
let checks = () => {
expect(profile.readOnly).toBeFalsy();
};

profile.verify().then(fail).catch(checks).then(done);
});

it('should remain dirty', (done) => {
let checks = () => {
expect(profile.dirty).toBeTruthy();
};

profile.verify().then(fail).catch(checks).then(done);
});
});
});
});

describe('level 2', () => {
let profile;

beforeEach(() => {
profile = new Profile(pendingVerificationUserObj, api);
});

it('should be read-only', () => {
expect(profile.readOnly).toBeTruthy();
});
});

describe('level 3', () => {
let profile;

beforeEach(() => {
profile = new Profile(verifiedUserObj, api);
});

it('should be read-only', () => {
expect(profile.readOnly).toBeTruthy();
});

it('sets the current limits', () => {
expect(profile.currentLimits.bank.inRemaining).toEqual(1000);
});
});
});

0 comments on commit 7efd2c3

Please sign in to comment.