-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrandom_example_spec.rb
65 lines (52 loc) · 2.17 KB
/
random_example_spec.rb
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
require "spec_helper"
RSpec.describe GovukSchemas::RandomExample do
describe ".for_schema" do
it "returns a random example for a schema" do
example = GovukSchemas::RandomExample.for_schema(frontend_schema: "generic")
expect(example).to be_a(Hash)
end
it "can be customised" do
example = GovukSchemas::RandomExample.for_schema(frontend_schema: "generic") do |hash|
hash.merge("base_path" => "/some-base-path")
end
expect(example["base_path"]).to eql("/some-base-path")
end
end
describe "#payload" do
GovukSchemas::Schema.all.each do |file_path, schema|
it "generates valid content for schema #{file_path}" do
# This will raise an informative error if an invalid schema is generated.
GovukSchemas::RandomExample.new(schema:).payload
end
end
it "returns the same output if a seed is detected" do
schema = GovukSchemas::Schema.random_schema(schema_type: "frontend")
first_payload = GovukSchemas::RandomExample.new(schema:, seed: 777).payload
second_payload = GovukSchemas::RandomExample.new(schema:, seed: 777).payload
expect(first_payload).to eql(second_payload)
end
it "can customise the payload" do
schema = GovukSchemas::Schema.random_schema(schema_type: "frontend")
example = GovukSchemas::RandomExample.new(schema:).payload do |hash|
hash.merge("base_path" => "/some-base-path")
end
expect(example["base_path"]).to eql("/some-base-path")
end
it "failes when attempting to edit the hash in place" do
schema = GovukSchemas::Schema.random_schema(schema_type: "frontend")
expect {
GovukSchemas::RandomExample.new(schema:).payload do |hash|
hash["base_path"] = "/some-base-path"
end
}.to raise_error(GovukSchemas::InvalidContentGenerated)
end
it "raises if the resulting content item won't be valid" do
schema = GovukSchemas::Schema.random_schema(schema_type: "frontend")
expect {
GovukSchemas::RandomExample.new(schema:).payload do |hash|
hash.merge("base_path" => nil)
end
}.to raise_error(GovukSchemas::InvalidContentGenerated)
end
end
end