Changeset View
Changeset View
Standalone View
Standalone View
swh/web/assets/src/bundles/webapp/tests/code-highlighting.test.js
- This file was added.
/** | |||||
* Copyright (C) 2019 The Software Heritage developers | |||||
* See the AUTHORS file at the top-level directory of this distribution | |||||
* License: GNU Affero General Public License version 3, or any later version | |||||
* See top-level LICENSE file for more information | |||||
*/ | |||||
import {setTestUrl, setTestDefaultUrl} from 'utils/test'; | |||||
const code = `#ifndef ADVICE_H | |||||
#define ADVICE_H | |||||
#include "git-compat-util.h" | |||||
extern int advice_push_update_rejected; | |||||
extern int advice_push_non_ff_current; | |||||
extern int advice_push_non_ff_matching; | |||||
extern int advice_push_already_exists; | |||||
extern int advice_push_fetch_first; | |||||
extern int advice_push_needs_force; | |||||
extern int advice_push_unqualified_ref_name; | |||||
extern int advice_status_hints; | |||||
extern int advice_status_u_option; | |||||
extern int advice_commit_before_merge; | |||||
extern int advice_reset_quiet_warning; | |||||
extern int advice_resolve_conflict; | |||||
extern int advice_implicit_identity; | |||||
extern int advice_detached_head; | |||||
extern int advice_set_upstream_failure; | |||||
extern int advice_object_name_warning; | |||||
extern int advice_amworkdir; | |||||
extern int advice_rm_hints; | |||||
extern int advice_add_embedded_repo; | |||||
extern int advice_ignored_hook; | |||||
extern int advice_waiting_for_editor; | |||||
extern int advice_graft_file_deprecated; | |||||
extern int advice_checkout_ambiguous_remote_branch_name; | |||||
int git_default_advice_config(const char *var, const char *value); | |||||
__attribute__((format (printf, 1, 2))) | |||||
void advise(const char *advice, ...); | |||||
int error_resolve_conflict(const char *me); | |||||
extern void NORETURN die_resolve_conflict(const char *me); | |||||
void NORETURN die_conclude_merge(void); | |||||
void detach_advice(const char *new_name); | |||||
#endif /* ADVICE_H */`; | |||||
const codeLines = code.split('\n'); | |||||
const html = ` | |||||
<!doctype html> | |||||
<html> | |||||
<head> | |||||
<meta charset="utf-8"> | |||||
</head> | |||||
<body> | |||||
<div class="swh-content"> | |||||
<div class="highlightjs"> | |||||
<pre><code class="cpp">${code}</code></pre> | |||||
</div> | |||||
</div> | |||||
</body> | |||||
</html> | |||||
`; | |||||
describe('Code highlighting tests', function() { | |||||
before(function() { | |||||
this.webapp = require('bundles/webapp'); | |||||
}); | |||||
beforeEach(function() { | |||||
document.body.innerHTML = html; | |||||
}); | |||||
it('should highlight source code and add line numbers', function(done) { | |||||
this.webapp.highlightCode(); | |||||
// give jsdom some time to execute asynchronous callbacks | |||||
setTimeout(() => { | |||||
let hljsLnNumbers = $('.hljs-ln-numbers'); | |||||
let hljsLnCode = $('.hljs-ln-code'); | |||||
assert.equal(hljsLnNumbers.length, codeLines.length); | |||||
assert.equal(hljsLnCode.length, codeLines.length); | |||||
done(); | |||||
}, 500); | |||||
}); | |||||
it('should only highlight source code', function(done) { | |||||
this.webapp.highlightCode(false); | |||||
// give jsdom some time to execute all js callbacks | |||||
setTimeout(() => { | |||||
let hljs = $('.hljs'); | |||||
let hljsLnNumbers = $('.hljs-ln-numbers'); | |||||
assert.equal(hljsLnNumbers.length, 0); | |||||
assert.equal(hljs.length, 1); | |||||
done(); | |||||
}, 500); | |||||
}); | |||||
it('should emphasize source code lines based on url fragment', function(done) { | |||||
let lineStart = 3; | |||||
let lineEnd = 7; | |||||
setTestUrl(`https://localhost/swh/code-highlighting/#L${lineStart}-L${lineEnd}`); | |||||
this.webapp.highlightCode(); | |||||
// give jsdom some time to execute all js callbacks | |||||
setTimeout(() => { | |||||
for (let i = 0; i < codeLines.length; ++i) { | |||||
let lineNumber = i + 1; | |||||
let line = $(`.hljs-ln-line[data-line-number="${lineNumber}"]`); | |||||
if (lineNumber >= lineStart && lineNumber <= lineEnd) { | |||||
assert.notEqual(line.css('background-color'), 'inherit'); | |||||
} else { | |||||
assert.equal(line.css('background-color'), 'inherit'); | |||||
} | |||||
} | |||||
setTestDefaultUrl(); | |||||
done(); | |||||
}, 500); | |||||
}); | |||||
}); |