This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
/
command-factory.spec.ts
210 lines (176 loc) · 10.7 KB
/
command-factory.spec.ts
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { SchemaNode } from "@connections/shared/schema-node.model";
import { CommandFactory } from "@dataservices/virtualization/view-editor/command/command-factory";
import { AddSourcesCommand } from "@dataservices/virtualization/view-editor/command/add-sources-command";
import { RemoveSourcesCommand } from "@dataservices/virtualization/view-editor/command/remove-sources-command";
import { NoOpCommand } from "@dataservices/virtualization/view-editor/command/no-op-command";
import { UpdateViewNameCommand } from "@dataservices/virtualization/view-editor/command/update-view-name-command";
import { UpdateViewDescriptionCommand } from "@dataservices/virtualization/view-editor/command/update-view-description-command";
describe( "Command Factory Tests", () => {
it("AddSourcesCommand Test", () => {
const conn1 = "conn1";
const conn2 = "conn2";
const path1 = "table=node1";
const path2 = "table=node2";
const node1 = SchemaNode.create( { connectionName: conn1, path: path1 } );
const node2 = SchemaNode.create( { connectionName: conn2, path: path2 } );
const tempCmd = CommandFactory.createAddSourcesCommand( [ node1, node2 ], "ident" );
expect( tempCmd instanceof AddSourcesCommand ).toBe( true );
const cmd = tempCmd as AddSourcesCommand;
expect( cmd.id ).toBe( AddSourcesCommand.id );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 2 );
const expectedSrcPaths = "connection=" + conn1 + "/" + path1 + ", connection=" + conn2 + "/" + path2;
const expectedFull = expectedSrcPaths + ", ObjectId=ident";
const actual = cmd.getArg( AddSourcesCommand.addedSourcePaths );
expect( cmd.getArg( AddSourcesCommand.addedSourcePaths ) ).toEqual( expectedSrcPaths );
expect( cmd.toString() ).toBe( "AddSourcesCommand, addedSourcePaths=" + expectedFull );
expect( cmd.isUndoable() ).toBe( true );
const json = cmd.toJSON();
const tempRoundtrip = CommandFactory.decode( json );
expect( tempRoundtrip instanceof AddSourcesCommand ).toBe( true );
const roundtrip = tempRoundtrip as AddSourcesCommand;
expect( roundtrip.id ).toBe( cmd.id );
expect( roundtrip.args.size ).toBe( cmd.args.size );
expect( roundtrip.getArg( AddSourcesCommand.addedSourcePaths ) ).toBe( cmd.getArg( AddSourcesCommand.addedSourcePaths ) );
});
it("AddSourcesCommand Undo Test", () => {
const node1 = SchemaNode.create( { connectionName: "conn1", path: "table=node1" } );
const node2 = SchemaNode.create( { connectionName: "conn2", path: "table=node2" } );
const tempCmd = CommandFactory.createAddSourcesCommand( [ node1, node2 ], "ident" );
expect( tempCmd instanceof AddSourcesCommand ).toBe( true );
const cmd = tempCmd as AddSourcesCommand;
const tempUndoCmd = CommandFactory.createUndoCommand( cmd );
expect( tempUndoCmd instanceof RemoveSourcesCommand ).toBe( true );
const undoCmd = tempUndoCmd as RemoveSourcesCommand;
expect( undoCmd ).not.toBeNull();
expect( undoCmd.id ).toBe( RemoveSourcesCommand.id );
expect( undoCmd.args.size ).toBe( 2 );
expect( undoCmd.getArg( RemoveSourcesCommand.removedSourcePaths ) ).toEqual( cmd.getArg( AddSourcesCommand.addedSourcePaths ) );
});
it("NoOpCommand Test", () => {
const cmd = CommandFactory.createNoOpCommand();
expect( cmd.id ).toBe( NoOpCommand.id );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 0 );
expect( cmd.toString() ).toEqual( "NoOpCommand, []" );
expect( cmd.isUndoable() ).toBe( false );
});
it("NoOpCommand Undo Test", () => {
const cmd = CommandFactory.createNoOpCommand();
const error = CommandFactory.createUndoCommand( cmd );
expect( error instanceof Error).toBe( true );
});
it("RemoveSourcesCommand Test", () => {
const conn1 = "conn1";
const conn2 = "conn2";
const path1 = "table=node1";
const path2 = "table=node2";
const node1 = SchemaNode.create( { connectionName: "conn1", path: path1 } );
const node2 = SchemaNode.create( { connectionName: "conn2", path: path2 } );
const temp = CommandFactory.createRemoveSourcesCommand( [ node1, node2 ], "ident" );
expect( temp instanceof RemoveSourcesCommand ).toBe( true );
const cmd = temp as RemoveSourcesCommand;
expect( cmd.id ).toBe( RemoveSourcesCommand.id );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 2 );
const expectedSrcPaths = "connection=" + conn1 + "/" + path1 + ", connection=" + conn2 + "/" + path2;
const expectedFull = expectedSrcPaths + ", ObjectId=ident";
expect( cmd.getArg( RemoveSourcesCommand.removedSourcePaths ) ).toEqual( expectedSrcPaths );
expect( cmd.toString() ).toEqual( "RemoveSourcesCommand, removedSourcePaths=" + expectedFull );
expect( cmd.isUndoable() ).toBe( true );
const json = cmd.toJSON();
const tempRoundtrip = CommandFactory.decode( json );
expect( tempRoundtrip instanceof RemoveSourcesCommand ).toBe( true );
const roundtrip = tempRoundtrip as RemoveSourcesCommand;
expect( roundtrip.id ).toBe( cmd.id );
expect( roundtrip.args.size ).toBe( cmd.args.size );
expect( roundtrip.getArg( RemoveSourcesCommand.removedSourcePaths ) ).toBe( cmd.getArg( RemoveSourcesCommand.removedSourcePaths ) );
});
it("RemoveSourcesCommand Undo Test", () => {
const node1 = SchemaNode.create( { connectionName: "conn1", path: "table=node1" } );
const node2 = SchemaNode.create( { connectionName: "conn2", path: "table=node2" } );
const tempCmd = CommandFactory.createRemoveSourcesCommand( [ node1, node2 ], "ident" );
expect( tempCmd instanceof RemoveSourcesCommand ).toBe( true );
const cmd = tempCmd as RemoveSourcesCommand;
const tempUndoCmd = CommandFactory.createUndoCommand( cmd );
expect( tempUndoCmd instanceof AddSourcesCommand ).toBe( true );
const undoCmd = tempUndoCmd as AddSourcesCommand;
expect( undoCmd ).not.toBeNull();
expect( undoCmd.id ).toBe( AddSourcesCommand.id );
expect( undoCmd.args.size ).toBe( 2 );
expect( undoCmd.getArg( AddSourcesCommand.addedSourcePaths ) ).toEqual( cmd.getArg( RemoveSourcesCommand.removedSourcePaths ) );
});
it("UpdateViewDescriptionCommand Test", () => {
const temp = CommandFactory.createUpdateViewDescriptionCommand( "theNewDescription",
"theOldDescription" );
expect( temp instanceof UpdateViewDescriptionCommand ).toBe( true );
const cmd = temp as UpdateViewDescriptionCommand;
expect( cmd.id ).toBe( UpdateViewDescriptionCommand.id );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 2 );
expect( cmd.getArg( UpdateViewDescriptionCommand.newDescription ) ).toEqual( "theNewDescription" );
expect( cmd.getArg( UpdateViewDescriptionCommand.oldDescription ) ).toEqual( "theOldDescription" );
expect( cmd.toString() ).toEqual( "UpdateViewDescriptionCommand, newDescription=theNewDescription, oldDescription=theOldDescription" );
expect( cmd.isUndoable() ).toBe( true );
const json = cmd.toJSON();
const tempRoundtrip = CommandFactory.decode( json );
expect( tempRoundtrip instanceof UpdateViewDescriptionCommand ).toBe( true );
const roundtrip = tempRoundtrip as UpdateViewDescriptionCommand;
expect( roundtrip.id ).toBe( cmd.id );
expect( roundtrip.args.size ).toBe( cmd.args.size );
expect( roundtrip.getArg( UpdateViewDescriptionCommand.newDescription ) )
.toBe( cmd.getArg( UpdateViewDescriptionCommand.newDescription ) );
expect( roundtrip.getArg( UpdateViewDescriptionCommand.oldDescription ) )
.toBe( cmd.getArg( UpdateViewDescriptionCommand.oldDescription ) );
});
it("UpdateViewDescriptionCommand Undo Test", () => {
const tempCmd = CommandFactory.createUpdateViewDescriptionCommand( "theNewDescription",
"theOldDescription" );
expect( tempCmd instanceof UpdateViewDescriptionCommand ).toBe( true );
const cmd = tempCmd as UpdateViewDescriptionCommand;
const tempUndoCmd = CommandFactory.createUndoCommand( cmd );
expect( tempUndoCmd instanceof UpdateViewDescriptionCommand ).toBe( true );
const undoCmd = tempUndoCmd as UpdateViewDescriptionCommand;
expect( undoCmd ).not.toBeNull();
expect( undoCmd.id ).toBe( UpdateViewDescriptionCommand.id );
expect( undoCmd.args.size ).toBe( 2 );
expect( undoCmd.getArg( UpdateViewDescriptionCommand.newDescription ) )
.toBe( cmd.getArg( UpdateViewDescriptionCommand.oldDescription ) );
expect( undoCmd.getArg( UpdateViewDescriptionCommand.oldDescription ) )
.toBe( cmd.getArg( UpdateViewDescriptionCommand.newDescription ) );
});
it("UpdateViewNameCommand Test", () => {
const newName = "theNewName";
const oldName = "theOldName";
const temp = CommandFactory.createUpdateViewNameCommand( newName, oldName );
expect( temp instanceof UpdateViewNameCommand ).toBe( true );
const cmd = temp as UpdateViewNameCommand;
expect( cmd.id ).toBe( UpdateViewNameCommand.id );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 2 );
expect( cmd.getArg( UpdateViewNameCommand.newName ) ).toEqual( newName );
expect( cmd.getArg( UpdateViewNameCommand.oldName ) ).toEqual( oldName );
expect( cmd.toString() ).toEqual( "UpdateViewNameCommand, newName=theNewName, oldName=theOldName" );
expect( cmd.isUndoable() ).toBe( true );
const json = cmd.toJSON();
const tempRoundtrip = CommandFactory.decode( json );
expect( tempRoundtrip instanceof UpdateViewNameCommand ).toBe( true );
const roundtrip = tempRoundtrip as UpdateViewNameCommand;
expect( roundtrip.getArg( UpdateViewNameCommand.newName ) ).toBe( cmd.getArg( UpdateViewNameCommand.newName ) );
expect( roundtrip.getArg( UpdateViewNameCommand.oldName ) ).toBe( cmd.getArg( UpdateViewNameCommand.oldName ) );
});
it("UpdateViewNameCommand Undo Test", () => {
const tempCmd = CommandFactory.createUpdateViewNameCommand( "theNewName",
"theOldName" );
expect( tempCmd instanceof UpdateViewNameCommand ).toBe( true );
const cmd = tempCmd as UpdateViewNameCommand;
const tempUndoCmd = CommandFactory.createUndoCommand( cmd );
expect( tempUndoCmd instanceof UpdateViewNameCommand ).toBe( true );
const undoCmd = tempUndoCmd as UpdateViewNameCommand;
expect( undoCmd ).not.toBeNull();
expect( undoCmd.id ).toBe( UpdateViewNameCommand.id );
expect( undoCmd.args.size ).toBe( 2 );
expect( undoCmd.getArg( UpdateViewNameCommand.newName ) ).toBe( cmd.getArg( UpdateViewNameCommand.oldName ) );
expect( undoCmd.getArg( UpdateViewNameCommand.oldName ) ).toBe( cmd.getArg( UpdateViewNameCommand.newName ) );
});
});