2024-04-29 00:17:15 +02:00
# frozen_string_literal: true
2023-10-20 22:50:39 +02:00
RSpec . describe TiptapService do
2024-01-18 12:38:46 +01:00
let ( :json ) do
{
type : 'doc' ,
content : [
{
type : 'header' ,
content : [
{
type : 'headerColumn' ,
content : [ { type : 'text' , text : 'Left' } ]
} ,
{
type : 'headerColumn' ,
content : [ { type : 'text' , text : 'Right' } ]
}
]
} ,
{
type : 'title' ,
content : [ { type : 'text' , text : 'Title' } ]
} ,
{
type : 'title' # remained empty in editor
} ,
{
type : 'heading' ,
attrs : { level : 2 , textAlign : 'center' } ,
content : [ { type : 'text' , text : 'Heading 2' } ]
} ,
{
type : 'heading' ,
2024-02-02 13:49:22 +01:00
attrs : { level : 3 , textAlign : 'center' } ,
2024-01-18 12:38:46 +01:00
content : [ { type : 'text' , text : 'Heading 3' } ]
} ,
{
type : 'heading' ,
attrs : { level : 3 } # remained empty in editor
} ,
{
type : 'paragraph' ,
attrs : { textAlign : 'right' } ,
content : [ { type : 'text' , text : 'First paragraph' } ]
} ,
{
type : 'paragraph' ,
content : [
{
type : 'text' ,
text : 'Bonjour ' ,
marks : [ { type : 'italic' } , { type : 'strike' } ]
} ,
{
type : 'mention' ,
2024-02-01 18:55:41 +01:00
attrs : { id : 'name' , label : 'Nom' } ,
2024-01-18 12:38:46 +01:00
marks : [ { type : 'bold' } , { type : 'underline' } ]
} ,
{
type : 'text' ,
text : ' '
} ,
{
type : 'text' ,
text : '!' ,
marks : [ { type : 'highlight' } ]
}
]
} ,
{
type : 'paragraph'
# no content, empty line
} ,
{
type : 'bulletList' ,
content : [
{
type : 'listItem' ,
content : [
{
type : 'paragraph' ,
content : [
{
type : 'text' ,
text : 'Item 1'
}
]
}
]
} ,
{
type : 'listItem' ,
content : [
{
type : 'paragraph' ,
content : [
{
type : 'text' ,
text : 'Item 2'
}
]
}
]
}
]
} ,
{
type : 'orderedList' ,
content : [
{
type : 'listItem' ,
content : [
{
type : 'paragraph' ,
content : [
{
type : 'text' ,
text : 'Item 1'
}
]
}
]
} ,
{
type : 'listItem' ,
content : [
{
type : 'paragraph' ,
content : [
{
type : 'text' ,
text : 'Item 2'
}
]
}
]
}
]
} ,
2024-07-04 21:02:07 +02:00
{
type : 'paragraph' ,
content : [
{
type : 'text' ,
text : 'Langages de prédilection:'
} ,
{
type : 'mention' ,
attrs : { id : 'languages' , label : 'Langages' }
}
]
} ,
2024-01-18 12:38:46 +01:00
{
type : 'footer' ,
content : [ { type : 'text' , text : 'Footer' } ]
}
]
}
end
2023-10-20 22:50:39 +02:00
describe '.to_html' do
2024-07-04 21:02:07 +02:00
let ( :substitutions ) { { 'name' = > 'Paul' , 'languages' = > ChampPresentations :: MultipleDropDownListPresentation . new ( [ 'ruby' , 'rust' ] ) } }
2023-10-20 22:50:39 +02:00
let ( :html ) do
[
2023-12-20 22:10:51 +01:00
'<header><div>Left</div><div>Right</div></header>' ,
2023-11-21 16:49:50 +01:00
'<h1>Title</h1>' ,
2024-02-02 13:49:22 +01:00
'<h2 class="body-start" style="text-align: center">Heading 2</h2>' ,
'<h3 style="text-align: center">Heading 3</h3>' ,
'<p style="text-align: right">First paragraph</p>' ,
2023-12-20 22:10:51 +01:00
'<p><s><em>Bonjour </em></s><u><strong>Paul</strong></u> <mark>!</mark></p>' ,
2023-10-20 22:50:39 +02:00
'<ul><li><p>Item 1</p></li><li><p>Item 2</p></li></ul>' ,
2023-11-21 16:49:50 +01:00
'<ol><li><p>Item 1</p></li><li><p>Item 2</p></li></ol>' ,
2024-08-30 13:00:13 +02:00
'<p>Langages de prédilection:</p><ul><li><p>ruby</p></li><li><p>rust</p></li></ul><p></p>' , # TODO: remove empty <p> ?
2023-11-21 16:49:50 +01:00
'<footer>Footer</footer>'
2023-10-20 22:50:39 +02:00
] . join
end
it 'returns html' do
2024-01-18 12:38:46 +01:00
expect ( described_class . new . to_html ( json , substitutions ) ) . to eq ( html )
end
2024-02-02 13:49:22 +01:00
context 'body start on paragraph' do
let ( :json ) do
{
type : 'doc' ,
content : [
{
type : 'title' ,
content : [ { type : 'text' , text : 'The Title' } ]
} ,
{
type : 'paragraph' ,
content : [ { type : 'text' , text : 'First paragraph' } ]
}
]
}
end
it 'defines stat body on first paragraph' do
expect ( described_class . new . to_html ( json , substitutions ) ) . to eq ( " <h1>The Title</h1><p class= \" body-start \" >First paragraph</p> " )
end
end
2024-07-25 11:42:50 +02:00
context 'ordered list with custom classes' do
let ( :json ) do
{
type : 'doc' ,
content : [
{
type : 'orderedList' ,
attrs : { class : " my-class " } ,
content : [
{
type : 'listItem' ,
content : [
{
type : 'paragraph' ,
content : [
{
type : 'text' ,
text : 'Item 1'
}
]
}
]
}
]
}
]
}
end
it " set class attribute " do
expect ( described_class . new . to_html ( json , substitutions ) ) . to eq ( '<ol class="my-class"><li><p>Item 1</p></li></ol>' )
end
end
2024-01-18 12:38:46 +01:00
end
describe '#used_tags' do
it 'returns used tags' do
2024-07-04 21:02:07 +02:00
expect ( described_class . used_tags_and_libelle_for ( json ) ) . to eq ( Set . new ( [ [ 'name' , 'Nom' ] , [ 'languages' , 'Langages' ] ] ) )
2023-10-20 22:50:39 +02:00
end
end
2024-03-02 22:41:05 +01:00
2024-07-15 23:22:04 +02:00
describe '.to_texts_and_tags' do
subject { described_class . new . to_texts_and_tags ( json , substitutions ) }
2024-03-02 22:41:05 +01:00
2024-07-15 23:22:04 +02:00
context 'nominal' do
let ( :json ) do
{
" content " = > [
{ " type " = > " paragraph " , " content " = > [ { " text " = > " export_ " , " type " = > " text " } , { " type " = > " mention " , " attrs " = > { " id " = > " dossier_number " , " label " = > " numéro du dossier " } } , { " text " = > " .pdf " , " type " = > " text " } ] }
]
} . deep_symbolize_keys
end
context 'with substitutions' do
let ( :substitutions ) { { " dossier_number " = > " 42 " } }
it 'returns texts_and_tags' do
is_expected . to eq ( " export_42.pdf " )
end
end
context 'without substitutions' do
let ( :substitutions ) { nil }
it 'returns texts_and_tags' do
is_expected . to eq ( " export_<span class='fr-tag fr-tag--sm'>numéro du dossier</span>.pdf " )
end
end
2024-03-02 22:41:05 +01:00
end
2024-07-15 23:22:04 +02:00
context 'empty paragraph' do
let ( :json ) { { content : [ { type : 'paragraph' } ] } }
let ( :substitutions ) { { } }
it { is_expected . to eq ( '' ) }
2024-03-02 22:41:05 +01:00
end
end
2023-10-20 22:50:39 +02:00
end