Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly store User Subclass in Storage #978

Merged
merged 6 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions integration/test/ParseUserTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const TestObject = Parse.Object.extend('TestObject');
class CustomUser extends Parse.User {
constructor(attributes) {
super(attributes);
this.className = 'CustomUser';
}

doSomething() {
Expand Down Expand Up @@ -630,6 +631,22 @@ describe('Parse User', () => {
expect(user.get('foo')).toBe('bar');
});

it('can get current with subclass', async () => {
Parse.User.enableUnsafeCurrentUser();

const customUser = new CustomUser({ foo: 'bar' });
customUser.setUsername('username');
customUser.setPassword('password');

await customUser.signUp();
Parse.User._clearCache();

const user = CustomUser.current();
expect(user instanceof CustomUser).toBe(true);
expect(user.doSomething()).toBe(5);
expect(user.get('foo')).toBe('bar');
});

it('can logIn user with subclass', async () => {
Parse.User.enableUnsafeCurrentUser();

Expand Down
2 changes: 1 addition & 1 deletion src/ParseUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ const DefaultController = {
updateUserOnDisk(user) {
const path = Storage.generatePath(CURRENT_USER_KEY);
const json = user.toJSON();
json.className = '_User';
json.className = user.className || '_User';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about

json.className = this.constructor.name === 'ParseUser' ? '_User' : this.constructor.name;

i think that'll work and then you don't have to worry about setting this.className.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

Copy link
Member Author

@dplewis dplewis Nov 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we have a flaky test. I can't restart the build the button is missing, can you?

Edit: Works now

return Storage.setItemAsync(
path, JSON.stringify(json)
).then(() => {
Expand Down