/
slugify.test.js
113 lines (99 loc) · 3.13 KB
/
slugify.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Dependencies
var util = require('util')
, assert = require('assert')
, mongoose = require('mongoose')
, slugify = require('../lib/slugify')
, common = require('./support/common')
, db = common.db
, cleanup = common.cleanup
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId
// Run tests
describe('Slugify', function() {
var FooSchema = new Schema()
describe('#default()', function() {
FooSchema.plugin(slugify)
var FooModel = mongoose.model('slugFoo', FooSchema)
, foo = new FooModel({ title: 'i like cookies!'})
before(function() {
FooModel.remove(function(err) {
assert.strictEqual(err, null)
})
})
it('should have custom properties', function(done) {
assert.strictEqual(typeof FooSchema.paths.title, 'object')
assert.strictEqual(typeof FooSchema.paths.slug, 'object')
assert.strictEqual(typeof FooSchema.methods.slugify, 'function')
done()
})
it('should slugify on save', function(done) {
foo.save(function(err, doc) {
assert.strictEqual(err, null)
assert.strictEqual(typeof doc.slug, 'string')
assert.strictEqual(doc.slug, 'i-like-cookies')
done()
})
})
it('should have the same value', function(done) {
foo.title = 'oh hai thar'
foo.save(function(err, doc) {
assert.strictEqual(err, null)
assert.strictEqual(typeof doc.slug, 'string')
assert.strictEqual(doc.slug, 'i-like-cookies')
done()
})
})
it('should manually slugify', function(done) {
var slug = foo.slugify('one two three')
assert.strictEqual(slug, 'one-two-three')
done()
})
})
describe('#custom()', function() {
FooSchema.plugin(slugify, {
source: 'hey'
, target: 'there'
, maxLength: 20
, invalidChar: 'i'
, spaceChar: '_'
, override: true
})
var BarModel = mongoose.model('slugBar', FooSchema)
, bar = new BarModel({
hey: 'lorem ipsum! twø a foo'
})
before(function() {
BarModel.remove(function(err) {
assert.strictEqual(err, null)
})
})
it('should have custom properties', function(done) {
assert.strictEqual(typeof FooSchema.paths.hey, 'object')
assert.strictEqual(typeof FooSchema.paths.there, 'object')
assert.strictEqual(typeof FooSchema.methods.slugify, 'function')
done()
})
it('should slugify on save', function(done) {
bar.save(function(err, doc) {
assert.strictEqual(err, null)
assert.strictEqual(typeof doc.there, 'string')
assert.strictEqual(doc.there, 'lorem_ipsumi_two_a_f')
done()
})
})
it('should create a new slug', function(done) {
bar.hey = 'well hëy there guy!!!'
bar.save(function(err, doc) {
assert.strictEqual(err, null)
assert.strictEqual(typeof doc.there, 'string')
assert.strictEqual(doc.there, 'well_hey_there_guyi')
done()
})
})
it('should manually slugify', function(done) {
var slug = bar.slugify('one two three')
assert.strictEqual(slug, 'one_two_three')
done()
})
})
})