Skip to content

Commit

Permalink
Merge pull request #368 from alphagov/use-govuk-frontend-v5-fix-inter…
Browse files Browse the repository at this point in the history
…mittent-tests

Fix intermittent test failures
  • Loading branch information
marcotranchino authored Oct 8, 2024
2 parents 8d407e8 + 96928c9 commit daec63f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 7 additions & 5 deletions lib/assets/javascripts/_modules/table-of-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
this.isMonitoring = false
}
StickyOverlapMonitors.prototype.run = function () {
var stickyIsVisible = this.$sticky.is(':visible')
var stickyIsVisible = this.$sticky.length > 0 && this.$sticky.is(':visible')
if (stickyIsVisible && !this.isMonitoring) {
document.addEventListener('focus', this.onFocus, true)
this.isMonitoring = true
Expand All @@ -26,11 +26,13 @@

if (!applicable) { return }

var stickyEdge = this.$sticky.get(0).getBoundingClientRect().bottom + this.offset
var diff = focused.getBoundingClientRect().top - stickyEdge
if (this.$sticky && this.$sticky.is(':visible') && this.$sticky.get(0)) {
var stickyEdge = this.$sticky.get(0).getBoundingClientRect().bottom + this.offset
var diff = focused.getBoundingClientRect().top - stickyEdge

if (diff < 0) {
$(window).scrollTop($(window).scrollTop() + diff)
if (diff < 0) {
$(window).scrollTop($(window).scrollTop() + diff)
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion spec/javascripts/table-of-contents-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('Table of contents', function () {
var $toc
var $closeButton
var $openButton
var $tocStickyHeader
var module

beforeAll(function () {
Expand Down Expand Up @@ -64,13 +65,18 @@ describe('Table of contents', function () {

$closeButton = $toc.find('.js-toc-close')
$openButton = $html.find('.js-toc-show')

$tocStickyHeader = $html.find('.toc-show')
})

afterEach(function () {
// clear up any classes left on <html>
$html.removeClass('.toc-open')
$html.removeClass('toc-open')
$html.find('body #toc-heading').remove()
$html.find('body .toc').remove()
if ($tocStickyHeader && $tocStickyHeader.length) {
$tocStickyHeader.remove()
}
})

describe('when the module is started', function () {
Expand Down

0 comments on commit daec63f

Please sign in to comment.