Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
scratch-www
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
scratch-www
Commits
53f6b133
Commit
53f6b133
authored
Oct 30, 2019
by
Ben Wheeler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
revised tests to distinguish between erroneous remote requests
parent
420e7dfa
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
104 additions
and
11 deletions
+104
-11
test/unit/components/username-step.test.jsx
test/unit/components/username-step.test.jsx
+104
-11
No files found.
test/unit/components/username-step.test.jsx
View file @
53f6b133
const
React
=
require
(
'
react
'
);
const
{
shallowWithIntl
}
=
require
(
'
../../helpers/intl-helpers.jsx
'
);
const
requestSuccessResponse
=
{
requestSucceeded
:
true
,
valid
:
false
,
errMsgId
:
'
registration.validationUsernameNotAllowed
'
};
const
requestFailureResponse
=
{
requestSucceeded
:
false
,
valid
:
false
,
errMsgId
:
'
general.error
'
};
// mockedValidateUsernameRemotely will return a promise resolving with remoteRequestResponse.
// Using remoteRequestResponse, rather than using requestSuccessResponse directly,
// lets us change where remoteRequestResponse points later, without actually changing
// mockedValidateUsernameRemotely.
let
remoteRequestResponse
=
requestSuccessResponse
;
let
mockedValidateUsernameRemotely
=
jest
.
fn
(()
=>
(
/* eslint-disable no-undef */
Promise
.
resolve
(
{
requestSucceeded
:
true
,
valid
:
false
,
errMsgId
:
'
registration.validationUsernameNotAllowed
'
}
)
Promise
.
resolve
(
remoteRequestResponse
)
/* eslint-enable no-undef */
));
...
...
@@ -19,10 +34,6 @@ const UsernameStep = require('../../../src/components/join-flow/username-step.js
describe
(
'
UsernameStep tests
'
,
()
=>
{
afterEach
(()
=>
{
jest
.
clearAllMocks
();
});
test
(
'
send correct props to formik
'
,
()
=>
{
const
wrapper
=
shallowWithIntl
(<
UsernameStep
/>);
const
formikWrapper
=
wrapper
.
dive
();
...
...
@@ -56,13 +67,11 @@ describe('UsernameStep tests', () => {
});
describe
(
'
validateUsernameRemotelyWithCache test
'
,
()
=>
{
describe
(
'
validateUsernameRemotelyWithCache test
with successful requests
'
,
()
=>
{
// mockedValidateUsernameRemotely = jest.fn(() => (
// /* eslint-disable no-undef */
// Promise.resolve({requestSucceeded: true, valid: false, errMsgId: 'registration.validationUsernameNotAllowed'})
// /* eslint-enable no-undef */
// ));
afterEach
(()
=>
{
jest
.
clearAllMocks
();
});
test
(
'
validateUsernameRemotelyWithCache calls validate.validateUsernameRemotely
'
,
done
=>
{
const
wrapper
=
shallowWithIntl
(<
UsernameStep
/>);
...
...
@@ -71,6 +80,7 @@ describe('validateUsernameRemotelyWithCache test', () => {
instance
.
validateUsernameRemotelyWithCache
(
'
newUniqueUsername55
'
)
.
then
(
response
=>
{
expect
(
mockedValidateUsernameRemotely
).
toHaveBeenCalled
();
expect
(
response
.
requestSucceeded
).
toBe
(
true
);
expect
(
response
.
valid
).
toBe
(
false
);
expect
(
response
.
errMsgId
).
toBe
(
'
registration.validationUsernameNotAllowed
'
);
done
();
...
...
@@ -86,6 +96,7 @@ describe('validateUsernameRemotelyWithCache test', () => {
instance
.
validateUsernameRemotelyWithCache
(
'
newUniqueUsername55
'
)
.
then
(
response
=>
{
expect
(
mockedValidateUsernameRemotely
).
toHaveBeenCalledTimes
(
1
);
expect
(
response
.
requestSucceeded
).
toBe
(
true
);
expect
(
response
.
valid
).
toBe
(
false
);
expect
(
response
.
errMsgId
).
toBe
(
'
registration.validationUsernameNotAllowed
'
);
})
...
...
@@ -94,6 +105,7 @@ describe('validateUsernameRemotelyWithCache test', () => {
instance
.
validateUsernameRemotelyWithCache
(
'
secondDifferent66
'
)
.
then
(
response
=>
{
expect
(
mockedValidateUsernameRemotely
).
toHaveBeenCalledTimes
(
2
);
expect
(
response
.
requestSucceeded
).
toBe
(
true
);
expect
(
response
.
valid
).
toBe
(
false
);
expect
(
response
.
errMsgId
).
toBe
(
'
registration.validationUsernameNotAllowed
'
);
done
();
...
...
@@ -110,6 +122,7 @@ describe('validateUsernameRemotelyWithCache test', () => {
instance
.
validateUsernameRemotelyWithCache
(
'
newUniqueUsername55
'
)
.
then
(
response
=>
{
expect
(
mockedValidateUsernameRemotely
).
toHaveBeenCalledTimes
(
1
);
expect
(
response
.
requestSucceeded
).
toBe
(
true
);
expect
(
response
.
valid
).
toBe
(
false
);
expect
(
response
.
errMsgId
).
toBe
(
'
registration.validationUsernameNotAllowed
'
);
})
...
...
@@ -118,6 +131,7 @@ describe('validateUsernameRemotelyWithCache test', () => {
instance
.
validateUsernameRemotelyWithCache
(
'
newUniqueUsername55
'
)
.
then
(
response
=>
{
expect
(
mockedValidateUsernameRemotely
).
toHaveBeenCalledTimes
(
1
);
expect
(
response
.
requestSucceeded
).
toBe
(
true
);
expect
(
response
.
valid
).
toBe
(
false
);
expect
(
response
.
errMsgId
).
toBe
(
'
registration.validationUsernameNotAllowed
'
);
done
();
...
...
@@ -125,3 +139,82 @@ describe('validateUsernameRemotelyWithCache test', () => {
});
});
});
describe
(
'
validateUsernameRemotelyWithCache test with failing requests
'
,
()
=>
{
beforeEach
(()
=>
{
// needs to be wrapped inside beforeEach, because if not, it gets run as this
// test file is loaded, and goes into effect before any of the earlier tests run!
remoteRequestResponse
=
requestFailureResponse
;
});
afterEach
(()
=>
{
jest
.
clearAllMocks
();
});
test
(
'
validateUsernameRemotelyWithCache calls validate.validateUsernameRemotely
'
,
done
=>
{
const
wrapper
=
shallowWithIntl
(<
UsernameStep
/>);
const
instance
=
wrapper
.
dive
().
instance
();
instance
.
validateUsernameRemotelyWithCache
(
'
newUniqueUsername55
'
)
.
then
(
response
=>
{
expect
(
mockedValidateUsernameRemotely
).
toHaveBeenCalled
();
expect
(
response
.
requestSucceeded
).
toBe
(
false
);
expect
(
response
.
valid
).
toBe
(
false
);
expect
(
response
.
errMsgId
).
toBe
(
'
general.error
'
);
done
();
});
});
test
(
'
validateUsernameRemotelyWithCache, called twice with different data, makes two remote requests
'
,
done
=>
{
const
wrapper
=
shallowWithIntl
(
<
UsernameStep
/>
);
const
instance
=
wrapper
.
dive
().
instance
();
instance
.
validateUsernameRemotelyWithCache
(
'
newUniqueUsername55
'
)
.
then
(
response
=>
{
expect
(
mockedValidateUsernameRemotely
).
toHaveBeenCalledTimes
(
1
);
expect
(
response
.
requestSucceeded
).
toBe
(
false
);
expect
(
response
.
valid
).
toBe
(
false
);
expect
(
response
.
errMsgId
).
toBe
(
'
general.error
'
);
})
.
then
(()
=>
{
// make the same request a second time
instance
.
validateUsernameRemotelyWithCache
(
'
secondDifferent66
'
)
.
then
(
response
=>
{
expect
(
mockedValidateUsernameRemotely
).
toHaveBeenCalledTimes
(
2
);
expect
(
response
.
requestSucceeded
).
toBe
(
false
);
expect
(
response
.
valid
).
toBe
(
false
);
expect
(
response
.
errMsgId
).
toBe
(
'
general.error
'
);
done
();
});
});
});
test
(
'
validateUsernameRemotelyWithCache, called 2x w/same data, makes 2 requests, since 1st not stored
'
,
done
=>
{
const
wrapper
=
shallowWithIntl
(
<
UsernameStep
/>
);
const
instance
=
wrapper
.
dive
().
instance
();
instance
.
validateUsernameRemotelyWithCache
(
'
newUniqueUsername55
'
)
.
then
(
response
=>
{
expect
(
mockedValidateUsernameRemotely
).
toHaveBeenCalledTimes
(
1
);
expect
(
response
.
requestSucceeded
).
toBe
(
false
);
expect
(
response
.
valid
).
toBe
(
false
);
expect
(
response
.
errMsgId
).
toBe
(
'
general.error
'
);
})
.
then
(()
=>
{
// make the same request a second time
instance
.
validateUsernameRemotelyWithCache
(
'
newUniqueUsername55
'
)
.
then
(
response
=>
{
expect
(
mockedValidateUsernameRemotely
).
toHaveBeenCalledTimes
(
2
);
expect
(
response
.
requestSucceeded
).
toBe
(
false
);
expect
(
response
.
valid
).
toBe
(
false
);
expect
(
response
.
errMsgId
).
toBe
(
'
general.error
'
);
done
();
});
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment