Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
63 changes: 63 additions & 0 deletions src/__tests__/ui/box/container.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ jest.mock('store/index', () => ({

jest.mock('ui/box/chrome', () => mockComponent('chrome'));

jest.mock('connection/database/index', () => ({
databaseUsernameValue: jest.fn()
}));

const mockEvent = {
preventDefault: () => {}
};
Expand Down Expand Up @@ -75,14 +79,25 @@ describe('Container', () => {
describe('with a custom `connectionResolver`', () => {
let connectionResolverMock;
let setResolvedConnectionMock;
let databaseUsernameValueMock;

beforeEach(() => {
connectionResolverMock = jest.fn();
setResolvedConnectionMock = jest.fn();
databaseUsernameValueMock = require('connection/database/index').databaseUsernameValue;

// Set default return value for databaseUsernameValue mock
databaseUsernameValueMock.mockReturnValue('peter_picked@pickledpepper.com');

require('core/index').connectionResolver = () => connectionResolverMock;
require('core/index').setResolvedConnection = setResolvedConnectionMock;
});

afterEach(() => {
// Reset mock between tests and restore default return value
databaseUsernameValueMock.mockReset().mockReturnValue('peter_picked@pickledpepper.com');
});

it('calls `connectionResolver` onSubmit', () => {
const c = getContainer();
c.handleSubmit(mockEvent);
Expand Down Expand Up @@ -110,6 +125,54 @@ describe('Container', () => {
expect(mock.calls.length).toBe(1);
expect(mock.calls[0]).toMatchSnapshot();
});

it('prioritizes email over username on signUp screen', () => {
databaseUsernameValueMock.mockReturnValue('test@example.com');

const c = getContainer({ screenName: 'main.signUp' });
c.handleSubmit(mockEvent);

// Should call databaseUsernameValue with emailFirst: true
expect(databaseUsernameValueMock).toHaveBeenCalledWith(
expect.anything(),
{ emailFirst: true }
);

// connectionResolver should receive the email value
const { mock } = connectionResolverMock;
expect(mock.calls[0][0]).toBe('test@example.com');
});

it('prioritizes username over email on login screen', () => {
databaseUsernameValueMock.mockReturnValue('testuser');

const c = getContainer({ screenName: 'main.login' });
c.handleSubmit(mockEvent);

// Should call databaseUsernameValue with empty options (default behavior)
expect(databaseUsernameValueMock).toHaveBeenCalledWith(
expect.anything(),
{}
);

// connectionResolver should receive the username value
const { mock } = connectionResolverMock;
expect(mock.calls[0][0]).toBe('testuser');
});

it('uses default behavior when screenName is not main.signUp', () => {
databaseUsernameValueMock.mockReturnValue('defaultvalue');

const c = getContainer({ screenName: 'forgotPassword' });
c.handleSubmit(mockEvent);

// Should call databaseUsernameValue with empty options (default behavior)
expect(databaseUsernameValueMock).toHaveBeenCalledWith(
expect.anything(),
{}
);
});

});

describe('when suppressSubmitOverlay is true', () => {
Expand Down
10 changes: 8 additions & 2 deletions src/ui/box/container.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CloseButton } from './button';
import * as l from '../../core/index';
import * as c from '../../field/index';
import { swap, updateEntity } from '../../store/index';
import { databaseUsernameValue } from '../../connection/database/index';

const badgeSvg = (
<svg focusable="false" width="58px" height="21px" viewBox="0 0 462 168">
Expand Down Expand Up @@ -69,15 +70,20 @@ export default class Container extends React.Component {
this.state = { isOpen: false };
}
checkConnectionResolver(done) {
const { contentProps } = this.props;
const { contentProps, screenName } = this.props;
const lock = contentProps.model;
const connectionResolver = l.connectionResolver(lock);
if (!connectionResolver) {
return done();
}
const { connections, id } = lock.get('client').toJS();
const context = { connections, id };
const userInputValue = c.getFieldValue(lock, 'username') || c.getFieldValue(lock, 'email');

// On signUp screen, use emailFirst option to prioritize email over username
// On login screen, use default behavior (username first)
const isSignUpScreen = screenName === 'main.signUp';
const userInputValue = databaseUsernameValue(lock, isSignUpScreen ? { emailFirst: true } : {});

connectionResolver(userInputValue, context, resolvedConnection => {
swap(updateEntity, 'lock', l.id(lock), m => l.setResolvedConnection(m, resolvedConnection));
done();
Expand Down