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
0036550a
Commit
0036550a
authored
Jul 30, 2020
by
picklesrus
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move timeout id out of state to a member variable and add some unittests.
parent
fac3ccad
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
19 deletions
+48
-19
src/components/navigation/www/navigation.jsx
src/components/navigation/www/navigation.jsx
+10
-17
test/unit/components/navigation.test.jsx
test/unit/components/navigation.test.jsx
+38
-2
No files found.
src/components/navigation/www/navigation.jsx
View file @
0036550a
...
...
@@ -30,15 +30,14 @@ class Navigation extends React.Component {
'
handleSearchSubmit
'
,
'
pollForMessages
'
]);
this
.
state
=
{
messageCountIntervalId
:
-
1
// javascript method interval id for getting messsage count.
};
// Keep the timeout id so we can cancel it (e.g. when we unmount)
this
.
messageCountTimeoutId
=
-
1
;
}
componentDidMount
()
{
if
(
this
.
props
.
user
)
{
// Setup polling for messages to start in 2 minutes.
const
twoMinInMs
=
2
*
60
*
1000
;
setTimeout
(
this
.
pollForMessages
.
bind
(
this
,
twoMinInMs
),
twoMinInMs
);
this
.
messageCountTimeoutId
=
setTimeout
(
this
.
pollForMessages
.
bind
(
this
,
twoMinInMs
),
twoMinInMs
);
}
}
componentDidUpdate
(
prevProps
)
{
...
...
@@ -46,25 +45,21 @@ class Navigation extends React.Component {
this
.
props
.
handleCloseAccountNav
();
if
(
this
.
props
.
user
)
{
const
twoMinInMs
=
2
*
60
*
1000
;
setTimeout
(
this
.
pollForMessages
.
bind
(
this
,
twoMinInMs
),
twoMinInMs
);
this
.
messageCountTimeoutId
=
setTimeout
(
this
.
pollForMessages
.
bind
(
this
,
twoMinInMs
),
twoMinInMs
);
}
else
{
// clear message count check, and set to default id.
clearTimeout
(
this
.
state
.
messageCountInterval
Id
);
clearTimeout
(
this
.
messageCountTimeout
Id
);
this
.
props
.
setMessageCount
(
0
);
this
.
setState
({
// eslint-disable-line react/no-did-update-set-state
messageCountIntervalId
:
-
1
});
this
.
messageCountTimeoutId
=
-
1
;
}
}
}
componentWillUnmount
()
{
// clear message interval if it exists
if
(
this
.
state
.
messageCountInterval
Id
!==
-
1
)
{
clearTimeout
(
this
.
state
.
messageCountInterval
Id
);
if
(
this
.
messageCountTimeout
Id
!==
-
1
)
{
clearTimeout
(
this
.
messageCountTimeout
Id
);
this
.
props
.
setMessageCount
(
0
);
this
.
setState
({
messageCountIntervalId
:
-
1
});
this
.
messageCountTimeoutId
=
-
1
;
}
}
getProfileUrl
()
{
...
...
@@ -82,9 +77,7 @@ class Navigation extends React.Component {
const
timeoutId
=
setTimeout
(()
=>
{
this
.
pollForMessages
(
nextFetch
);
},
nextFetch
);
this
.
setState
({
// eslint-disable-line react/no-did-mount-set-state
messageCountIntervalId
:
timeoutId
});
this
.
messageCountTimeoutId
=
timeoutId
;
}
}
...
...
test/unit/components/navigation.test.jsx
View file @
0036550a
...
...
@@ -128,14 +128,49 @@ describe('Navigation', () => {
childContextTypes
:
{
store
}
});
intlWrapper
.
children
().
find
(
'
Navigation
'
)
const
navInstance
=
intlWrapper
.
children
().
find
(
'
Navigation
'
)
.
instance
();
const
twoMin
=
2
*
60
*
1000
;
expect
(
setTimeout
).
toHaveBeenLastCalledWith
(
expect
.
any
(
Function
),
twoMin
);
expect
(
navInstance
.
messageCountTimeoutId
).
not
.
toEqual
(
-
1
);
// Advance timers passed the intial two minutes.
jest
.
advanceTimersByTime
(
twoMin
+
1
);
expect
(
setTimeout
).
toHaveBeenLastCalledWith
(
expect
.
any
(
Function
),
twoMin
*
2
);
expect
(
props
.
getMessageCount
).
toHaveBeenCalled
();
expect
(
navInstance
.
messageCountTimeoutId
).
not
.
toEqual
(
-
1
);
});
test
(
'
Component cancels timers when it unmounts
'
,
()
=>
{
store
=
mockStore
({
navigation
:
{
registrationOpen
:
false
},
messageCount
:
{
messageCount
:
5
}
});
const
props
=
{
user
:
{
thumbnailUrl
:
'
scratch.mit.edu
'
,
username
:
'
auser
'
},
getMessageCount
:
jest
.
fn
()
};
const
intlWrapper
=
mountWithIntl
(
<
Navigation
{
...
props
}
/>,
{
context
:
{
store
},
childContextTypes
:
{
store
}
});
const
navInstance
=
intlWrapper
.
children
().
find
(
'
Navigation
'
)
.
instance
();
const
twoMin
=
2
*
60
*
1000
;
expect
(
setTimeout
).
toHaveBeenLastCalledWith
(
expect
.
any
(
Function
),
twoMin
);
expect
(
navInstance
.
messageCountTimeoutId
).
not
.
toEqual
(
-
1
);
debugger
;
navInstance
.
componentWillUnmount
();
expect
(
clearTimeout
).
toHaveBeenCalledWith
(
expect
.
any
(
Number
));
expect
(
navInstance
.
messageCountTimeoutId
).
toEqual
(
-
1
);
});
test
(
'
pollForMessages polls for messages 5 times
'
,
()
=>
{
...
...
@@ -170,8 +205,8 @@ describe('Navigation', () => {
let
twoMinInMs
=
2
*
60
*
1000
;
// 2 minutes in ms.
navInstance
.
pollForMessages
(
twoMinInMs
);
expect
(
navInstance
.
messageCountTimeoutId
).
not
.
toEqual
(
-
1
);
// Check that we set the timeout to backoff exponentially
for
(
let
count
=
1
;
count
<
5
;
++
count
)
{
jest
.
advanceTimersByTime
(
twoMinInMs
+
1
);
expect
(
setTimeout
).
toHaveBeenLastCalledWith
(
expect
.
any
(
Function
),
twoMinInMs
*
2
);
...
...
@@ -187,5 +222,6 @@ describe('Navigation', () => {
// setTimeout happens 1 fewer since the original call to
// pollForMessages isn't counted here.
expect
(
setTimeout
).
toHaveBeenCalledTimes
(
4
);
expect
(
navInstance
.
messageCountTimeoutId
).
not
.
toEqual
(
-
1
);
});
});
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