diff --git a/common/models/templates/src/common.ts b/common/models/templates/src/common.ts index fe3b285c7c..73d360a2e7 100644 --- a/common/models/templates/src/common.ts +++ b/common/models/templates/src/common.ts @@ -71,11 +71,15 @@ namespace models { export function transformToSuggestion(transform: Transform): Suggestion; export function transformToSuggestion(transform: Transform, p: number): Suggestion & {p: number}; export function transformToSuggestion(transform: Transform, p?: number): Suggestion & {p?: number} { - return { + let suggestion: Suggestion & {p?: number} = { transform: transform, transformId: transform.id, - displayAs: transform.insert, - p: p + displayAs: transform.insert }; + + if(p === 0 || p) { + suggestion.p = p; + } + return suggestion; } } diff --git a/common/models/templates/test/test-common.js b/common/models/templates/test/test-common.js new file mode 100644 index 0000000000..61892ad985 --- /dev/null +++ b/common/models/templates/test/test-common.js @@ -0,0 +1,70 @@ +/* + * Unit tests for common utility functions/methods. + */ + +var assert = require('chai').assert; +var models = require('../').models; + +describe('Common utility functions', function() { + // TODO: unit tests for other common utility functions + + describe('transformToSuggestion', function() { + it('p: undefined', function() { + let suggestion = { + transform: { + insert: 'hello', + deleteLeft: 0, + id: 0 + }, + transformId: 0, + displayAs: 'hello' + }; + + assert.deepEqual(models.transformToSuggestion(suggestion.transform), suggestion); + }); + + it('p: 0', function() { + let suggestion = { + transform: { + insert: 'hello', + deleteLeft: 0, + id: 0 + }, + transformId: 0, + displayAs: 'hello', + p: 0 + }; + + assert.deepEqual(models.transformToSuggestion(suggestion.transform, 0), suggestion); + }); + + it('p > 0', function() { + let suggestion = { + transform: { + insert: 'hello', + deleteLeft: 0, + id: 0 + }, + transformId: 0, + displayAs: 'hello', + p: 0.5 + }; + + assert.deepEqual(models.transformToSuggestion(suggestion.transform, 0.5), suggestion); + }); + + it('properly handles the transformId', function() { + let suggestion = { + transform: { + insert: 'hello', + deleteLeft: 0, + id: 3 + }, + transformId: 3, // Ensures there isn't a separate ID seed in use. + displayAs: 'hello' + }; + + assert.deepEqual(models.transformToSuggestion(suggestion.transform), suggestion); + }); + }); +}); diff --git a/common/models/templates/test/test-quote-behavior.js b/common/models/templates/test/test-quote-behavior.js index 80964b7a7f..339cf0af9d 100644 --- a/common/models/templates/test/test-quote-behavior.js +++ b/common/models/templates/test/test-quote-behavior.js @@ -1,5 +1,5 @@ /* - * Unit tests for the priority queue. + * Unit tests for quote behaviors. */ var assert = require('chai').assert;