|
| 1 | +// Copyright IBM Corp. and LoopBack contributors 2018,2026. All Rights Reserved. |
| 2 | +// Node module: @loopback/example-todo |
| 3 | +// This file is licensed under the MIT License. |
| 4 | +// License text available at https://opensource.org/licenses/MIT |
| 5 | + |
| 6 | +import {expect} from '@loopback/testlab'; |
| 7 | +import {Todo} from '../../../models'; |
| 8 | + |
| 9 | +describe('Todo (unit)', () => { |
| 10 | + describe('constructor', () => { |
| 11 | + it('creates an instance with no data', () => { |
| 12 | + const todo = new Todo(); |
| 13 | + expect(todo).to.be.instanceOf(Todo); |
| 14 | + }); |
| 15 | + |
| 16 | + it('creates an instance with partial data', () => { |
| 17 | + const todo = new Todo({ |
| 18 | + title: 'Test Todo', |
| 19 | + }); |
| 20 | + expect(todo.title).to.equal('Test Todo'); |
| 21 | + expect(todo.id).to.be.undefined(); |
| 22 | + expect(todo.desc).to.be.undefined(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('creates an instance with complete data', () => { |
| 26 | + const data = { |
| 27 | + id: 1, |
| 28 | + title: 'Test Todo', |
| 29 | + desc: 'Test Description', |
| 30 | + isComplete: false, |
| 31 | + remindAtAddress: '123 Main St, City, 12345', |
| 32 | + remindAtGeo: '40.7128,-74.0060', |
| 33 | + tag: {priority: 'high'}, |
| 34 | + }; |
| 35 | + const todo = new Todo(data); |
| 36 | + expect(todo.id).to.equal(1); |
| 37 | + expect(todo.title).to.equal('Test Todo'); |
| 38 | + expect(todo.desc).to.equal('Test Description'); |
| 39 | + expect(todo.isComplete).to.be.false(); |
| 40 | + expect(todo.remindAtAddress).to.equal('123 Main St, City, 12345'); |
| 41 | + expect(todo.remindAtGeo).to.equal('40.7128,-74.0060'); |
| 42 | + expect(todo.tag).to.deepEqual({priority: 'high'}); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('properties', () => { |
| 47 | + it('has optional id property', () => { |
| 48 | + const todo = new Todo({title: 'Test'}); |
| 49 | + expect(todo).to.not.have.property('id'); |
| 50 | + |
| 51 | + todo.id = 1; |
| 52 | + expect(todo.id).to.equal(1); |
| 53 | + }); |
| 54 | + |
| 55 | + it('has required title property', () => { |
| 56 | + const todo = new Todo({title: 'Required Title'}); |
| 57 | + expect(todo.title).to.equal('Required Title'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('has optional desc property', () => { |
| 61 | + const todo = new Todo({title: 'Test'}); |
| 62 | + expect(todo.desc).to.be.undefined(); |
| 63 | + |
| 64 | + todo.desc = 'Description'; |
| 65 | + expect(todo.desc).to.equal('Description'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('has optional isComplete property', () => { |
| 69 | + const todo = new Todo({title: 'Test'}); |
| 70 | + expect(todo.isComplete).to.be.undefined(); |
| 71 | + |
| 72 | + todo.isComplete = true; |
| 73 | + expect(todo.isComplete).to.be.true(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('has optional remindAtAddress property', () => { |
| 77 | + const todo = new Todo({title: 'Test'}); |
| 78 | + expect(todo.remindAtAddress).to.be.undefined(); |
| 79 | + |
| 80 | + todo.remindAtAddress = '123 Main St'; |
| 81 | + expect(todo.remindAtAddress).to.equal('123 Main St'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('has optional remindAtGeo property', () => { |
| 85 | + const todo = new Todo({title: 'Test'}); |
| 86 | + expect(todo.remindAtGeo).to.be.undefined(); |
| 87 | + |
| 88 | + todo.remindAtGeo = '40.7128,-74.0060'; |
| 89 | + expect(todo.remindAtGeo).to.equal('40.7128,-74.0060'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('has optional tag property of any type', () => { |
| 93 | + const todo = new Todo({title: 'Test'}); |
| 94 | + expect(todo.tag).to.be.undefined(); |
| 95 | + |
| 96 | + todo.tag = {priority: 'high', category: 'work'}; |
| 97 | + expect(todo.tag).to.deepEqual({priority: 'high', category: 'work'}); |
| 98 | + |
| 99 | + todo.tag = 'simple-tag'; |
| 100 | + expect(todo.tag).to.equal('simple-tag'); |
| 101 | + |
| 102 | + todo.tag = ['tag1', 'tag2']; |
| 103 | + expect(todo.tag).to.deepEqual(['tag1', 'tag2']); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('data validation', () => { |
| 108 | + it('allows empty description', () => { |
| 109 | + const todo = new Todo({ |
| 110 | + title: 'Test', |
| 111 | + desc: '', |
| 112 | + }); |
| 113 | + expect(todo.desc).to.equal(''); |
| 114 | + }); |
| 115 | + |
| 116 | + it('allows false for isComplete', () => { |
| 117 | + const todo = new Todo({ |
| 118 | + title: 'Test', |
| 119 | + isComplete: false, |
| 120 | + }); |
| 121 | + expect(todo.isComplete).to.be.false(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('allows true for isComplete', () => { |
| 125 | + const todo = new Todo({ |
| 126 | + title: 'Test', |
| 127 | + isComplete: true, |
| 128 | + }); |
| 129 | + expect(todo.isComplete).to.be.true(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('handles complex tag objects', () => { |
| 133 | + const complexTag = { |
| 134 | + priority: 'high', |
| 135 | + labels: ['urgent', 'important'], |
| 136 | + metadata: { |
| 137 | + createdBy: 'user1', |
| 138 | + assignedTo: ['user2', 'user3'], |
| 139 | + }, |
| 140 | + }; |
| 141 | + const todo = new Todo({ |
| 142 | + title: 'Test', |
| 143 | + tag: complexTag, |
| 144 | + }); |
| 145 | + expect(todo.tag).to.deepEqual(complexTag); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('edge cases', () => { |
| 150 | + it('handles very long titles', () => { |
| 151 | + const longTitle = 'A'.repeat(1000); |
| 152 | + const todo = new Todo({title: longTitle}); |
| 153 | + expect(todo.title).to.equal(longTitle); |
| 154 | + expect(todo.title.length).to.equal(1000); |
| 155 | + }); |
| 156 | + |
| 157 | + it('handles very long descriptions', () => { |
| 158 | + const longDesc = 'B'.repeat(5000); |
| 159 | + const todo = new Todo({ |
| 160 | + title: 'Test', |
| 161 | + desc: longDesc, |
| 162 | + }); |
| 163 | + expect(todo.desc).to.equal(longDesc); |
| 164 | + expect(todo.desc!.length).to.equal(5000); |
| 165 | + }); |
| 166 | + |
| 167 | + it('handles special characters in title', () => { |
| 168 | + const specialTitle = 'Test @#$%^&*() 测试 🎉'; |
| 169 | + const todo = new Todo({title: specialTitle}); |
| 170 | + expect(todo.title).to.equal(specialTitle); |
| 171 | + }); |
| 172 | + |
| 173 | + it('handles multiline descriptions', () => { |
| 174 | + const multilineDesc = 'Line 1\nLine 2\nLine 3'; |
| 175 | + const todo = new Todo({ |
| 176 | + title: 'Test', |
| 177 | + desc: multilineDesc, |
| 178 | + }); |
| 179 | + expect(todo.desc).to.equal(multilineDesc); |
| 180 | + }); |
| 181 | + |
| 182 | + it('handles null-like values gracefully', () => { |
| 183 | + const todo = new Todo({ |
| 184 | + title: 'Test', |
| 185 | + desc: undefined, |
| 186 | + isComplete: undefined, |
| 187 | + }); |
| 188 | + expect(todo.desc).to.be.undefined(); |
| 189 | + expect(todo.isComplete).to.be.undefined(); |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + describe('model inheritance', () => { |
| 194 | + it('extends Entity class', () => { |
| 195 | + const todo = new Todo({title: 'Test'}); |
| 196 | + expect(todo).to.have.property('toJSON'); |
| 197 | + expect(todo).to.have.property('toObject'); |
| 198 | + }); |
| 199 | + |
| 200 | + it('can be serialized to JSON', () => { |
| 201 | + const todo = new Todo({ |
| 202 | + id: 1, |
| 203 | + title: 'Test Todo', |
| 204 | + desc: 'Description', |
| 205 | + isComplete: false, |
| 206 | + }); |
| 207 | + const json = todo.toJSON(); |
| 208 | + expect(json).to.have.property('id', 1); |
| 209 | + expect(json).to.have.property('title', 'Test Todo'); |
| 210 | + expect(json).to.have.property('desc', 'Description'); |
| 211 | + expect(json).to.have.property('isComplete', false); |
| 212 | + }); |
| 213 | + }); |
| 214 | +}); |
| 215 | + |
| 216 | +// Made with Bob |
0 commit comments