codecombat/test/app/lib/LevelLoader.spec.coffee

182 lines
5.8 KiB
CoffeeScript
Raw Normal View History

Level = require 'models/Level'
LevelSession = require 'models/LevelSession'
SuperModel = require 'models/SuperModel'
LevelComponent = require 'models/LevelComponent'
LevelLoader = require 'lib/LevelLoader'
# LEVELS
levelWithOgreWithMace = {
thangs: [{
thangType: 'ogre'
components: [{
original: LevelComponent.EquipsID
majorVersion: 0
config: { inventory: { 'left-hand': 'mace' } }
}]
}]
}
levelWithShaman = {
thangs: [{
thangType: 'shaman'
}]
}
levelWithShamanWithSuperWand = {
thangs: [{
thangType: 'shaman'
components: [{
original: LevelComponent.EquipsID
majorVersion: 0
config: { inventory: { 'left-hand': 'super-wand' } }
}]
}]
}
# SESSIONS
sessionWithTharinWithHelmet = { heroConfig: { thangType: 'tharin', inventory: { 'head': 'helmet' }}}
sessionWithAnyaWithGloves = { heroConfig: { thangType: 'anya', inventory: { 'head': 'gloves' }}}
# THANG TYPES
thangTypeOgreWithPhysicalComponent = {
name: 'Ogre'
original: 'ogre'
components: [{
original: 'physical'
majorVersion: 0
}]
}
thangTypeShamanWithWandEquipped = {
name: 'Shaman'
original: 'shaman'
components: [{
original: LevelComponent.EquipsID
majorVersion: 0
config: { inventory: { 'left-hand': 'wand' }}
}]
}
thangTypeTharinWithHealsComponent = {
name: 'Tharin'
original: 'tharin'
components: [{
original: 'heals'
majorVersion: 0
}]
}
thangTypeWand = {
name: 'Wand'
original: 'wand'
components: [{
original: 'poisons'
majorVersion: 0
}]
}
thangTypeAnyaWithJumpsComponent = {
name: 'Anya'
original: 'anya'
components: [{
original: 'jumps'
majorVersion: 0
}]
}
describe 'LevelLoader', ->
describe 'loadDependenciesForSession', ->
it 'loads hero and item thang types from heroConfig in the given session', ->
levelLoader = new LevelLoader({supermodel:new SuperModel(), sessionID: 'id', levelID: 'id'})
session = new LevelSession(sessionWithAnyaWithGloves)
levelLoader.loadDependenciesForSession(session)
requests = jasmine.Ajax.requests.all()
urls = (r.url for r in requests)
expect('/db/thang.type/gloves/version?project=name,components,original' in urls).toBeTruthy()
2014-09-28 13:47:01 -04:00
expect('/db/thang.type/anya/version' in urls).toBeTruthy()
it 'loads components for the hero in the heroConfig in the given session', ->
levelLoader = new LevelLoader({supermodel:new SuperModel(), sessionID: 'id', levelID: 'id'})
session = new LevelSession(sessionWithAnyaWithGloves)
levelLoader.loadDependenciesForSession(session)
responses = {
2014-09-28 13:47:01 -04:00
'/db/thang.type/anya/version': thangTypeAnyaWithJumpsComponent
}
jasmine.Ajax.requests.sendResponses(responses)
requests = jasmine.Ajax.requests.all()
urls = (r.url for r in requests)
expect('/db/level.component/jumps/version/0' in urls).toBeTruthy()
it 'is idempotent', ->
levelLoader = new LevelLoader({supermodel:new SuperModel(), sessionID: 'id', levelID: 'id'})
# first load Tharin by the 'normal' session load
responses = '/db/level/id': levelWithOgreWithMace
jasmine.Ajax.requests.sendResponses(responses)
responses = '/db/level_session/id': sessionWithTharinWithHelmet
jasmine.Ajax.requests.sendResponses(responses)
# then try to load Tharin some more
session = new LevelSession(sessionWithTharinWithHelmet)
levelLoader.loadDependenciesForSession(session)
2014-09-28 13:47:01 -04:00
numRequestsBefore = jasmine.Ajax.requests.count()
levelLoader.loadDependenciesForSession(session)
levelLoader.loadDependenciesForSession(session)
numRequestsAfter = jasmine.Ajax.requests.count()
expect(numRequestsAfter).toBe(numRequestsBefore)
it 'loads thangs for items that the level thangs have in their Equips component configs', ->
new LevelLoader({supermodel:supermodel = new SuperModel(), sessionID: 'id', levelID: 'id'})
responses = {
'/db/level/id': levelWithOgreWithMace
}
jasmine.Ajax.requests.sendResponses(responses)
requests = jasmine.Ajax.requests.all()
urls = (r.url for r in requests)
2014-08-13 13:48:22 -04:00
expect('/db/thang.type/mace/version?project=name,components,original' in urls).toBeTruthy()
it 'loads components which are inherited by level thangs from thang type default components', ->
new LevelLoader({supermodel:new SuperModel(), sessionID: 'id', levelID: 'id'})
responses =
'/db/level/id': levelWithOgreWithMace
'/db/thang.type/names': [thangTypeOgreWithPhysicalComponent]
jasmine.Ajax.requests.sendResponses(responses)
requests = jasmine.Ajax.requests.all()
urls = (r.url for r in requests)
expect('/db/level.component/physical/version/0' in urls).toBeTruthy()
it 'loads item thang types which are inherited by level thangs from thang type default equips component configs', ->
new LevelLoader({supermodel:new SuperModel(), sessionID: 'id', levelID: 'id'})
responses =
'/db/level/id': levelWithShaman
'/db/thang.type/names': [thangTypeShamanWithWandEquipped]
jasmine.Ajax.requests.sendResponses(responses)
requests = jasmine.Ajax.requests.all()
urls = (r.url for r in requests)
2014-08-13 13:48:22 -04:00
expect('/db/thang.type/wand/version?project=name,components,original' in urls).toBeTruthy()
it 'loads components for item thang types which are inherited by level thangs from thang type default equips component configs', ->
new LevelLoader({supermodel:new SuperModel(), sessionID: 'id', levelID: 'id'})
responses =
'/db/level/id': levelWithShaman
'/db/thang.type/names': [thangTypeShamanWithWandEquipped]
2014-08-13 13:48:22 -04:00
'/db/thang.type/wand/version?project=name,components,original': thangTypeWand
jasmine.Ajax.requests.sendResponses(responses)
requests = jasmine.Ajax.requests.all()
urls = (r.url for r in requests)
expect('/db/level.component/poisons/version/0' in urls).toBeTruthy()