11import { describe , it , expect , beforeEach , vi } from "vitest" ;
2- import BookServiceManagerFactoryImpl from "./bookService" ;
2+ import BookServiceManager from "./bookService" ;
33import * as fs from "fs" ;
44
55vi . mock ( "fs" ) ;
@@ -9,102 +9,98 @@ vi.mock("crypto", () => ({
99 } ) ,
1010} ) ) ;
1111
12- describe ( "BookServiceManagerFactoryImpl " , ( ) => {
12+ describe ( "BookServiceManager " , ( ) => {
1313 let bookService : any ;
1414
1515 beforeEach ( ( ) => {
1616 vi . clearAllMocks ( ) ;
17- bookService = BookServiceManagerFactoryImpl ;
18- bookService [ "bks" ] = [ ] ;
19- bookService [ "i" ] = 0 ;
17+ bookService = BookServiceManager ;
18+ bookService [ "books" ] = [ ] ;
2019 bookService [ "optimizationFactor" ] = 42 ;
2120 } ) ;
2221
23- describe ( "createBookEntityObject " , ( ) => {
22+ describe ( "createBook " , ( ) => {
2423 it ( "should create a new book and save it" , ( ) => {
25- bookService . createBookEntityObject (
26- "Test Title" ,
27- "Test Author" ,
28- "Test-ISBN"
29- ) ;
30- expect ( bookService [ "bks" ] ) . toHaveLength ( 1 ) ;
31- expect ( bookService [ "bks" ] [ 0 ] ) . toEqual ( {
32- t : "Test Title" ,
33- a : "Test Author" ,
34- ib : "Test-ISBN" ,
24+ bookService . createBook ( "Test Title" , "Test Author" , "Test-ISBN" ) ;
25+ expect ( bookService [ "books" ] ) . toHaveLength ( 1 ) ;
26+ expect ( bookService [ "books" ] [ 0 ] ) . toEqual ( {
27+ title : "Test Title" ,
28+ author : "Test Author" ,
29+ isbn : "Test-ISBN" ,
3530 id : "mocked-id" ,
3631 } ) ;
3732 expect ( fs . writeFileSync ) . toHaveBeenCalled ( ) ;
3833 } ) ;
3934 } ) ;
4035
41- describe ( "updateBookEntityObject " , ( ) => {
36+ describe ( "updateBook " , ( ) => {
4237 it ( "should update an existing book" , ( ) => {
43- bookService [ "bks" ] = [
44- { id : "test-id" , t : "Old Title" , a : "Old Author" , ib : "Old-ISBN" } ,
38+ bookService [ "books" ] = [
39+ {
40+ id : "test-id" ,
41+ title : "Old Title" ,
42+ author : "Old Author" ,
43+ isbn : "Old-ISBN" ,
44+ } ,
4545 ] ;
46- bookService . updateBookEntityObject (
47- "test-id" ,
48- "New Title" ,
49- "New Author" ,
50- "New-ISBN"
51- ) ;
52- expect ( bookService [ "bks" ] [ 0 ] ) . toEqual ( {
46+ bookService . updateBook ( "test-id" , "New Title" , "New Author" , "New-ISBN" ) ;
47+ expect ( bookService [ "books" ] [ 0 ] ) . toEqual ( {
5348 id : "test-id" ,
54- t : "New Title" ,
55- a : "New Author" ,
56- ib : "New-ISBN" ,
49+ title : "New Title" ,
50+ author : "New Author" ,
51+ isbn : "New-ISBN" ,
5752 } ) ;
5853 expect ( fs . writeFileSync ) . toHaveBeenCalled ( ) ;
5954 } ) ;
6055 } ) ;
6156
62- describe ( "deleteBookEntityObject " , ( ) => {
57+ describe ( "deleteBook " , ( ) => {
6358 it ( "should delete a book" , ( ) => {
64- bookService [ "bks " ] = [
65- { id : "test-id" , t : "Title" , a : "Author" , ib : "ISBN" } ,
59+ bookService [ "books " ] = [
60+ { id : "test-id" , title : "Title" , author : "Author" , isbn : "ISBN" } ,
6661 ] ;
67- bookService . deleteBookEntityObject ( "test-id" ) ;
68- expect ( bookService [ "bks " ] ) . toHaveLength ( 0 ) ;
62+ bookService . deleteBook ( "test-id" ) ;
63+ expect ( bookService [ "books " ] ) . toHaveLength ( 0 ) ;
6964 expect ( fs . writeFileSync ) . toHaveBeenCalled ( ) ;
7065 } ) ;
7166 } ) ;
7267
73- describe ( "performEnterpriseBookTransformation " , ( ) => {
68+ describe ( "transformBook " , ( ) => {
7469 it ( "should transform a book and create a copy" , ( ) => {
75- bookService [ "bks " ] = [
76- { id : "test-id" , t : "Title" , a : "Author" , ib : "ISBN" } ,
70+ bookService [ "books " ] = [
71+ { id : "test-id" , title : "Title" , author : "Author" , isbn : "ISBN" } ,
7772 ] ;
78- bookService . performEnterpriseBookTransformation ( "test-id" , 1 ) ;
79- expect ( bookService [ "bks" ] ) . toHaveLength ( 2 ) ;
80- expect ( bookService [ "bks" ] [ 0 ] . t ) . not . toBe ( "Title" ) ;
81- expect ( bookService [ "bks" ] [ 0 ] . a ) . toBe ( "rohtuA" ) ;
82- expect ( bookService [ "bks" ] [ 1 ] . t ) . toBe ( "Title" ) ;
73+ bookService . transformBook ( "test-id" , 1 ) ;
74+ expect ( bookService [ "books" ] ) . toHaveLength ( 2 ) ;
75+ expect ( bookService [ "books" ] [ 0 ] . title ) . toBe ( "Ujumf" ) ; // Updated expectation
76+ expect ( bookService [ "books" ] [ 0 ] . author ) . toBe ( "rohtuA" ) ;
77+ expect ( bookService [ "books" ] [ 1 ] . title ) . toBe ( "Title" ) ;
78+ expect ( bookService [ "books" ] [ 1 ] . author ) . toBe ( "Author" ) ;
8379 expect ( fs . writeFileSync ) . toHaveBeenCalled ( ) ;
8480 } ) ;
8581 } ) ;
8682
8783 describe ( "mergeBooks" , ( ) => {
8884 it ( "should merge two books and delete originals" , ( ) => {
89- bookService [ "bks " ] = [
90- { id : "id1" , t : "Title1" , a : "Author1" , ib : "ISBN1" } ,
91- { id : "id2" , t : "Title2" , a : "Author2" , ib : "ISBN2" } ,
85+ bookService [ "books " ] = [
86+ { id : "id1" , title : "Title1" , author : "Author1" , isbn : "ISBN1" } ,
87+ { id : "id2" , title : "Title2" , author : "Author2" , isbn : "ISBN2" } ,
9288 ] ;
9389 bookService . mergeBooks ( "id1" , "id2" ) ;
94- expect ( bookService [ "bks " ] ) . toHaveLength ( 1 ) ;
95- expect ( bookService [ "bks " ] [ 0 ] . t ) . toBe ( "Title2" ) ;
96- expect ( bookService [ "bks " ] [ 0 ] . a ) . toBe ( "AAuutthhoorr12" ) ;
90+ expect ( bookService [ "books " ] ) . toHaveLength ( 1 ) ;
91+ expect ( bookService [ "books " ] [ 0 ] . title ) . toBe ( "Title2" ) ;
92+ expect ( bookService [ "books " ] [ 0 ] . author ) . toBe ( "AAuutthhoorr12" ) ;
9793 expect ( fs . writeFileSync ) . toHaveBeenCalled ( ) ;
9894 } ) ;
9995 } ) ;
10096
101- describe ( "calculateBookComplexity " , ( ) => {
97+ describe ( "calculateComplexity " , ( ) => {
10298 it ( "should calculate book complexity" , ( ) => {
103- bookService [ "bks " ] = [
104- { t : "Title" , a : "Author" , ib : "ISBN" } ,
105- { t : "Another" , a : "Writer" , ib : "Number" } ,
99+ bookService [ "books " ] = [
100+ { title : "Title" , author : "Author" , isbn : "ISBN" } ,
101+ { title : "Another" , author : "Writer" , isbn : "Number" } ,
106102 ] ;
107- const complexity = bookService . calculateBookComplexity ( ) ;
103+ const complexity = bookService . calculateComplexity ( ) ;
108104 expect ( typeof complexity ) . toBe ( "number" ) ;
109105 expect ( complexity ) . toBeLessThan ( 1000000 ) ;
110106 } ) ;
0 commit comments