kadamwhite/twentyfourteen-enhanced-image-nav — pull request #4 by kadamwhite (2013-11-30 07:38:21+00:00)
Title: Update the <title> element when loading new images via AJAX

We get a little TDD on this one, writing ourselves a breaking unit test before doing any actual implementation:

```javascript
test( 'Content requests update the document

', function() {
TwentyFourteen.navigate( 'some/url/' );
this.requests[0].respond( 200, {
'Content-Type': 'text/html'
}, '<title>New Title

' );

strictEqual( document.title, 'New Title',
'Page title is updated based on retrieved content' );
});
```

With the test in place, we can then write the actual logic that will make that test pass:

```
var updateTitle = function( responseText ) {
// Parse the response as raw text to find a title tag
var titleRegEx = /

([^<]*)<\/title>/i,
titleMatch = responseText.match( titleRegEx ),
// If regex hits a match, titleMatch will not be null
title = titleMatch !== null ? titleMatch[1] : false;

// If we find a match, update the document's title attribute
if ( title ) {
document.title = title;
}
};
```

This method of writing tests first may not always suit your development environment, but at the very least it can be beneficial to stub out a series of test *descriptions* that can serve as a to-do list while you are developing. Here's an example of these tests written out in that manner:

```
// Won't run or throw errors, but can serve as a useful TODO list
test( 'Left key navigates to previous image', 0, jQuery.noop );
test( 'Right key navigates to next image', 0, jQuery.noop );
test( 'No navigation occurs if textarea or input have focus', 0, jQuery.noop );
test( 'Requested content is loaded into the #content container', 0, jQuery.noop );
test( 'Content requests update the document <title>', 0, jQuery.noop );
```

`jQuery.noop` is a function with no operations ("no-op"), which is necessary because QUnit expects each test to have a argument that is a function. The `0` means that each test expects 0 assertions.